我创建了一个考勤管理系统,我想从表中检索不同类型的日期。喜欢,
在我的考勤表中,有不同的日期,但有一些日期是重复的-对于每个日期,都有一定数量的用户有考勤
staff table. attendance table
id = 1 id = 1
name = john staff_id = 1
date = 31.08.2021
id = 2 attendance = present
name = harry
id = 2
staff_id = 2
date = 31.08.2021
attendance = absent
id = 3
staff_id = 1
date = 01.09.2021
attendance = present
id = 4
staff_id = 2
date = 01.09.2021
attendance = present从上面的表数据中,我想检索唯一没有重复的日期
因此,当我们编写代码时,答案将是
31.08.2021
01.09.2021这是我的模板文件
{% for attendances in attendance %}
<th>{{ attendances.date }}</th>
// here i want to retrive 31.08.2021 and 01.09.2021
{% endfor %}这是我的视图文件
def index(request):
staffs = staff.objects.all()
attendances = attendance.objects.all()
date = datetime.date.today()
return render(request, "salary/index.html", {'staff': staffs, 'attendance': attendances, 'date': date})发布于 2021-09-01 07:07:32
您可以直接从数据库获取它(如果您使用postgres),或者从attendances自己准备它。做对你最好的事情。
def index(request):
staffs = staff.objects.all()
attendances = attendance.objects.all()
date = datetime.date.today()
# unique_dates = list(attendance.objects.all().order_by().values('date').distinct())
# OR
# unique_dates = list({a.date for a in atteendances})
return render(request, "salary/index.html", {'staff': staffs, 'attendance': attendances, 'date': date, 'unique_dates': unique_dates})https://stackoverflow.com/questions/69008594
复制相似问题