Django:查询的天真日期时间

时间:2013-03-21 14:22:19

标签: python django datetime timezone

我正在尝试检索两个日期之间的所有数据(星期一 - >星期一)。这是我的代码:

    some_day_last_week = datetime.datetime.now() - timedelta(days=7)
    monday_of_last_week = some_day_last_week - timedelta(days=(some_day_last_week.isocalendar()[2] - 1))
    monday_of_this_week = monday_of_last_week + timedelta(days=7)

    print "Someday last week %s" % some_day_last_week
    print "Monday of last week %s" % monday_of_last_week
    print "Monday of this week %s" % monday_of_this_week
    jobs = Jobs.objects.filter(created_time__gte=monday_of_last_week, created_time__lt=monday_of_this_week).order_by('-created_time')

    for job in jobs:
        print "Jobb: %s (%s)" % (job, job.created_time)

输出很好,但是我得到了一个让我烦恼的RuntimeWarning。这是(以及其他输出):

Someday last week 2013-03-14 15:11:32.819508
Monday of last week 2013-03-11 15:11:32.819508
Monday of this week 2013-03-18 15:11:32.819508
xxx/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2013-03-11 15:11:32.819508) while time zone support is active.

RuntimeWarning)     xxx / venv / lib / python2.7 / site-packages / django / db / models / fields / init .py:808:RuntimeWarning:DateTimeField收到一个天真的日期时间(2013-03-18 15: 11:32.819508)时区支持处于活动状态。   RuntimeWarning)     Jobb:Webdesigner(2013-03-13 09:14:02 + 00:00)     [21 / Mar / 2013 15:11:32]“GET / jobs / feed / 7days / HTTP / 1.1”200 712

我在settings.py中将USE_TZ设置为True,而created_time字段是这样的:

created_time = models.DateTimeField(_('Creation time'), default=datetime.now)

任何? :)

1 个答案:

答案 0 :(得分:1)

jobs = Jobs.objects.filter(
    created_time__range[monday_of_last_week, monday_of_this_week]).order_by('-created_time')