Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中过滤datetime字段_Python_Django - Fatal编程技术网

Python 如何在django中过滤datetime字段

Python 如何在django中过滤datetime字段,python,django,Python,Django,我有一个名为Task的模型,在queryset函数中,我想通过时间过滤器获得所有任务,我想过滤(任务的结束时间在现在之前) 下面是代码,但现在可以工作了,筛选后没有任务显示 EstEndTime = models.DateTimeField('End Time', null=True) def queryset(self, request, queryset): from django.utils import timezone now = timezone.now()

我有一个名为Task的模型,在queryset函数中,我想通过时间过滤器获得所有任务,我想过滤(任务的结束时间在现在之前)

下面是代码,但现在可以工作了,筛选后没有任务显示

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)

这将只返回现在创建的项目(精确到微秒,具体取决于您的数据库)

您可能正在寻找以下内容:

queryset.filter(EstEndTime__lte=now)

请注意,将字段命名为
EstEndTime
是违反Django(以及Python)命名约定的,我建议使用如下命名模式:
est\u end\u time

这将只返回现在创建的项目(精确到微秒,具体取决于您的数据库)

您可能正在寻找以下内容:

queryset.filter(EstEndTime__lte=now)
作为记录,将字段命名为
EstEndTime
是违反Django(以及Python)命名约定的,我建议使用如下命名模式:
est\u end\u time