Django: Naive datetime on query
我正在尝试检索两个日期(星期一->星期一)之间的所有数据。这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 | 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) |
输出很好,但是我收到了一个运行时警告,这让我很头疼。这是(以及其他输出):
1 2 3 4 | 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. |
号
运行时警告)当时区支持处于活动状态时,xxx/venv/lib/python2.7/site packages/django/db/models/fields/init.py:808:runtimewarning:datetimefield接收到一个简单的日期时间(2013-03-18 15:11:32.819508)。运行时警告)Jobb:网页设计师(2013-03-13 09:14:02+00:00)[2013年3月21日15:11:32]"get/jobs/feed/7days/http/1.1"200 712
我在settings.py中使用了"tz"设置为"true",创建的字段"time"如下:
1 | created_time = models.DateTimeField(_('Creation time'), default=datetime.now) |
有人吗?:)
1 2 | jobs = Jobs.objects.filter( created_time__range[monday_of_last_week, monday_of_this_week]).order_by('-created_time') |