首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在django中检索不同类型的数据?

如何在django中检索不同类型的数据?
EN

Stack Overflow用户
提问于 2021-09-01 06:11:05
回答 1查看 35关注 0票数 1

我创建了一个考勤管理系统,我想从表中检索不同类型的日期。喜欢,

在我的考勤表中,有不同的日期,但有一些日期是重复的-对于每个日期,都有一定数量的用户有考勤

代码语言:javascript
运行
复制
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

从上面的表数据中,我想检索唯一没有重复的日期

因此,当我们编写代码时,答案将是

代码语言:javascript
运行
复制
31.08.2021
01.09.2021

这是我的模板文件

代码语言:javascript
运行
复制
{% for attendances in attendance %}
    <th>{{ attendances.date }}</th>
    // here i want to retrive 31.08.2021 and 01.09.2021
{% endfor %}

这是我的视图文件

代码语言:javascript
运行
复制
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})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-01 07:07:32

您可以直接从数据库获取它(如果您使用postgres),或者从attendances自己准备它。做对你最好的事情。

代码语言:javascript
运行
复制
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})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69008594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档