简体   繁体   English

如何过滤django中的datetime字段

[英]How to filter the datetime field in django

I have a model named Task, and in the queryset function, I want to get all the tasks by the filter of time, I want to filter (Task's end time is before now) 我有一个名为Task的模型,在queryset函数中,我想通过过滤器获取所有任务的时间,我想过滤(任务的结束时间是之前的)

Below is the code, but this can now work, no tasks show after filter. 下面是代码,但现在可以使用,过滤后没有显示任务。

EstEndTime = models.DateTimeField('End Time', null=True)

def queryset(self, request, queryset):
    from django.utils import timezone
    now = timezone.now()
    return queryset.filter(Owner=str(current_user_name),EstEndTime = now)

This will only return items created exactly now (to the microsecond precise depending on your database). 这将只返回现在精确创建的项目(精确到微秒,具体取决于您的数据库)。

You are probably looking for this: 你可能正在寻找这个:

queryset.filter(EstEndTime__lte=now)

For the record, having fields named EstEndTime is against Django (and Python for that matter) naming conventions, I would recommend using a naming pattern like: est_end_time 对于记录,具有名为EstEndTime字段是针对Django(以及Python)的命名约定,我建议使用命名模式,如: est_end_time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM