-1

我想将查询集中的 DateField 和 TimeField 与当前日期进行比较。我搜索了几个小时,但没有找到任何东西。使用 Q/F-Object 进行了很多尝试,但也没有解决方案。现在我在这里,希望有人知道如何解决这个问题:) - 顺便说一句。拆分为日期和时间不是我的错,也无法将其更改为 DateTimeField(在其他项目中依赖太多)。

class Model(models.Model):
    date = models.DateField()
    time = models.TimeField()

在 MySQL 中,我会执行以下操作:
SELECT * FROM app_model WHERE CAST(CONCAT(CAST(date as CHAR),' ',CAST(time as CHAR)) as DATETIME) >= NOW()

感谢您的任何建议!

4

1 回答 1

0

You can do this with or'd queryset contraints (Q objects):

now = datetime.datetime.now()
future_models = Model.objects.filter(Q(date__gt=now.date()) | (Q(date=now.date()) & Q(time__gte=now.time())))

That selects all instances for which date is past today, and all instances for which date is today and the the time is greater than or equal to the current time.

于 2013-08-15T22:00:48.680 回答