如何从 Django 的 DateTimeField 中过滤一个多小时前创建的对象查询集?

How to filter a queryset of objects created more than one hour ago from Django's DateTimeField?

问题:

我正在尝试筛选状态在一个多小时内未更改的模型。

我试过的:

Product.objects.filter(
                Q(status="PENDING"),
                Q(created__hour__gt=1)
            ).all().order_by("-created")

预期解决方案: 获取状态为“PENDING”但在一个多小时内未更改的对象的查询集。

您使用以下方式过滤:

from datetime import timedelta
from django.db.models.functions import Now

Product.objects.filter(
    status="PENDING", <strong>created__lt=Now()-timedelta(hours=1)</strong>
).order_by('-created')