我正在尝试检索两个日期(星期一-> 星期一)之间的所有数据。这是我的代码:
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)
任何人?:)