Django模型中的日期过滤器会导致意外行为

时间:2015-01-20 05:37:54

标签: python django

这似乎很容易,但它给我带来了困惑。我的代码是

today = datetime.datetime.now()
StatusObj = Status.objects.filter(taskPeople__people__email = useremail,dateCreated__year = today.year, dateCreated__month = today.month, dateCreated__day = today.day) 

预计它将过滤日期为今天的查询。但它不会过滤而是过滤一天后的查询。 当我这样做的时候。

today.day
>> 20
Status.objects.filter(taskPeople__people__email = useremail,dateCreated__year = today.year, dateCreated__month = today.month, dateCreated__day = today.day)[0].dateCreated.day
>>19

1 个答案:

答案 0 :(得分:1)

您应该使用django.utils.timezone.now代替datetime.datetime.now

from django.utils import timezone
today = timezone.now()
...

阅读时区文档的troubleshooting部分中的问题#3。