2013-03-21 172 views
0

我試圖檢索兩個日期(星期一 - >星期一)之間的所有數據。這裏是我的代碼:Django:查詢的天真日期時間

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這是我的bug。下面是(以及其他輸出):

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 /站點包/ Django的/ DB /模型/場/ 初始化的.py:808 :RuntimeWarning:DateTimeField在時區支持處於活動狀態時收到了天真的日期時間(2013-03-18 15:11:32.819508)。 (2013-03-13 09:14:02 + 00:00) [21/Mar/2013 15:11:32]「GET/jobs/feed/7days/HTTP/1.1」200 712

我USE_TZ設置爲True settings.py中,和現場CREATED_TIME是這樣的:

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

有人嗎? :)

回答

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

全局名稱'created_time__range'未定義。不應該__range開箱即用? :) – Mats 2013-03-21 14:43:32

+0

啊,你錯過了=。但仍然,我得到相同的運行時警告..:/ – Mats 2013-03-21 14:47:27

+0

爲什麼會發生這種情況,它是一個內置的查詢功能。 hmmmmmm .... – catherine 2013-03-21 14:48:22

相關問題