django south migration and USE_TZ=True

爱⌒轻易说出口 提交于 2019-12-10 11:40:36

问题


I've changed all my code to use aware-time by using django.utils.timezone.now() I changed datetimefield to use default=timezone.now, and set USE_TZ=True in settings.py

After the changes, I ran south schemamigration command and it doesn't pick up the database fields change.

I'm using the south 0.7.6 and postgresql if that matters.

Here's the detailed change I made to make my entire site timezone aware.
how do I make my site timezone aware?


回答1:


If you use south, and change USE_TZ=False to USE_TZ=True, all your old migrations will likely break, and you'll get errors like:

RuntimeWarning: DateTimeField received a naive datetime (XXX)
while time zone support is active

I have not found a way to have south track changes in settings.py. What I've done is alter the actual migration files. These each have a number and are in the migrations subdirectory.

For each one that fails, add from django.utils import timezone, and modify any DateTimeField fields the same way as you did in your main code (making them time zone aware, or eliminating a specific non-timezone default date).

Alternately you can try to reset your south migration history.




回答2:


Building on Bryce's answer, these are the bash scripts we used to convert our south migrations to timezone-aware:

$ cd mysite/mainapp/migrations
$ find ./ -type f -exec sed -i -e 's/datetime.datetime.now/timezone.now/g' {} \;
$ find ./ -type f -exec sed -i -e '/import datetime/{G;s/$/from django.utils import timezone/;}' {} \;

Used on OS X 10.11.5 "El Capitan."



来源:https://stackoverflow.com/questions/17667527/django-south-migration-and-use-tz-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!