Django过滤器“小于”日期时间无法正常运行

时间:2019-03-14 21:10:31

标签: python django datetime filter django-queryset

我正在尝试通过时间戳(小于某个值)过滤Django查询集。但是,筛选器似乎允许通过的记录不少于指定的时间戳。这是一个示例函数:

def check_jobs_status_3():
    current_time = datetime.utcnow()
    time_threshold = current_time - timedelta(seconds=60)
    print("$$$$$$$$$$$$ current_time = {}, timedelta = {}, time_threshold = {}".format(current_time,timedelta(seconds=60),time_threshold))
    stuck_jobs_qs = Job.objects.filter(last_updated__lt=time_threshold)
    for stuck_job in stuck_jobs_qs:
        print("############################## Job #{} (last_updated = {}) no encoding status after {} seconds. Re-enqueueing.".format(stuck_job.id,stuck_job.last_updated,get_video_encoding_timeout_seconds()))

以下是输出:

$$$$$$$$$$$$ current_time = 2019-03-14 20:54:15.221554, timedelta = 0:01:00, time_threshold = 2019-03-14 20:53:15.221554
############################## Job #20 (last_updated = 2019-03-14 20:54:15.221264+00:00) no encoding status after 60 seconds. Re-enqueueing.

如您所见,我收到的记录的last_updated设置为2019-03-14 20:54:15,该记录不小于2019-03-14 20:53:15的过滤器值

这是last_updated字段的定义:

last_updated = models.DateTimeField(auto_now=True)

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

使用Django的时区感知方法django.utils.timezone.now

coords