2016-01-22 88 views
1

我開始使用Django構建的項目,我無法弄清楚什麼。Django:make_aware期待一個天真的日期時間

我在settings.py:

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

在我的models.py,我有日期字段是這樣的:

#... 
date = models.DateTimeField(default=timezone.now) 
created_at = models.DateTimeField(auto_now_add=True) 
updated_at = models.DateTimeField(auto_now=True) 
#... 

而且我的網址都是這樣在urls.py定義:

urlpatterns = patterns(
    '', 
    url(r'^api/catalog/', include('catalog.urls')), 
    url(r'^api/logs/', include('logs.urls')), 
    url(r'^admin/', include(admin.site.urls)), 
) 

而在目錄/ urls.py:

router = DefaultRouter(trailing_slash=False) 
router.register(r'users', UserViewSet) 
# ... 
projects_router = NestedSimpleRouter(router, r'projects', lookup='project', trailing_slash=False) 
projects_router.register(r'requests', ProjectRequestViewSet, base_name='project-requests') 
# ... 
requests_router = NestedSimpleRouter(router, r'requests', lookup='request', trailing_slash=False) 
requests_router.register(r'statuses', RequestStatusViewSet, base_name='request-statuses') 
# ... 
urlpatterns = [ 
    url(r'^', include(router.urls)), 
    url(r'^', include(projects_router.urls)), 
    url(r'^', include(requests_router.urls)), 
    url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), 
    url(r'^token-auth', views.obtain_auth_token), 
] 

既然我已經更新到1.9.1 Django的,我有以下警告:

.\env\lib\site-packages\django\template\utils.py:37:

RemovedInDjango110Warning: You haven't defined a TEMPLATES setting. You must do so before upgrading to Django 1.10. Otherwise Django will be unable to load templates. "unable to load templates.", RemovedInDjango110Warning)

.\balrog\urls.py:8: RemovedInDjango110Warning:

django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead. url(r'^admin/', include(admin.site.urls)),

(這不是這個問題的要點,但任何幫助去除這些警告也表示讚賞。 )

每當我試圖得到一個對象與API的日期字段,我得到以下錯誤:

ValueError: make_aware expects a naive datetime, got 2016-01-15 17:18:44.258843+00:00

所以不會發生,如果這個錯誤設置爲False但這並不理想,我需要它是True

另一種方法消除這種誤差是編輯.\env\Lib\site-packages\django\utils\timezone.py 並更改該make_aware功能:

def make_aware(value, timezone=None, is_dst=None): 
    """ 
    Makes a naive datetime.datetime in a given time zone aware. 
    """ 
    if timezone is None: 
     timezone = get_current_timezone() 
    if hasattr(timezone, 'localize'): 
     # This method is available for pytz time zones. 
     return timezone.localize(value, is_dst=is_dst) 
    else: 
     # Check that we won't overwrite the timezone of an aware datetime. 
     if is_aware(value): 
      raise ValueError(
       "make_aware expects a naive datetime, got %s" % value) 
     # This may be wrong around DST changes! 
     return value.replace(tzinfo=timezone) 

要這樣:

def make_aware(value, timezone=None, is_dst=None): 
    """ 
    Makes a naive datetime.datetime in a given time zone aware. 
    """ 
    if timezone is None: 
     timezone = get_current_timezone() 
    if hasattr(timezone, 'localize'): 
     # This method is available for pytz time zones. 
     return timezone.localize(value, is_dst=is_dst) 
    else: 
     return value 

但是,這並不理想都不是。

爲什麼我的make_aware函數甚至被調用,因爲日期應該已經知道並且不是天真USE_TZ設置爲True

順便說一句,我使用的是在這的有關情況SQLite數據庫(因爲sqlite的使用日期字段作爲字符串據我所知)

回答

0

好吧,它實際上,因爲在我的數據庫中的某些日期手動插入作爲一個字符串和SQLite沒有發揮作用,刪除數據庫並使用Django ORM重新構建它解決了這個問題。

相關問題