Default value of DateTimeField for South migration in Django project with activated timezone support

爷,独闯天下 提交于 2019-12-04 05:11:54
dgel

Manually edit the migration file that South created and add:

from django.utils import timezone

Then find the field that you are adding in the migration file and set its default to timezone.now().

pytz can be used for making timezone-aware datetime objects. you can use the following in your migration file:

import pytz
cn_tz = pytz.timezone('Asia/Shanghai')

then in your model

self.gf('django.db.models.fields.DateTimeField')(auto_now=True, default=datetime.datetime.now(cn_tz), blank=True),

I noticed when I ran migrations on dev machine, it was different than when I ran them on production.

My dev machine at the top of the file has

    from south_utils import datetime_utils as datetime

where on production it generated

    import datetime

By replacing the latter with the former, it solved my issue with no additional edits to the migration file.

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