I started to work on a project built with Django and I can't figure something out.
I have in my settings.py :
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
In my models.py, I have date fields like this :
#...
date = models.DateTimeField(default=timezone.now)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
#...
And my urls are defined like this in 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)),
)
And in catalog/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),
]
And since I have updated to Django 1.9.1, I have the following warnings :
.\\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)),
(That's not the main point of this question, but any help to remove these warnings is also appreciated.)
And whenever I'm trying to get an object with a date field with the API, I get the following error :
ValueError: make_aware expects a naive datetime, got 2016-01-15 17:18:44.258843+00:00
So this error doesn't occur if USE_TZ
is set to False
but that's not ideal, I need it to be True
.
Another way to remove this error is to edit .\\env\\Lib\\site-packages\\django\\utils\\timezone.py
And change this make_aware function :
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)
To this :
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
But that's not ideal neither.
Why is my make_aware function even called since the dates should already be aware and not naive with USE_TZ
set to True
?
Btw, I'm using a SQLite database in case that's related (since sqlite use date field as string afaik)
好的,实际上是因为我的数据库中的某些日期是作为字符串手动插入的,而SQLite并没有什么不同,因此清除数据库并使用Django ORM重建它可以解决此问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.