我有一个这样的模型:
class Menu(models.Model):
version = models.BigIntegerField(blank=False, null=False)
local = models.ForeignKey(Local, db_column='id_local')
created = models.DateTimeField(auto_now_add=True, blank=True,db_column='created')
actived = models.DateTimeField(db_column='actived')
class Meta:
managed = False
db_table = 'menu'
当我创建一个对象时;一切都简单又顺利。但我在更新“actived”时遇到问题。我使用如下代码:
menu = Menu.objects.get(version=ver, local=local)
menu.actived = datetime.now()
menu.save()
激活值正确;当我在数据库(MySql,使用 utf-8 字符集)或模板中看到它时,该值对应。但“menu.created”的值更改为“menu.created - 1 HOUR”对应的值。
我不知道为什么;这就是我修改的所有代码。
最佳答案
终于,我找到了解决方案。
首先;谢谢凯文·克里斯托弗·亨利。
在我的 settings.py 中,我更改了这 3 个设置,并且一切正常。
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = False
关于mysql - Django DatetimeField 在更新时发生更改,值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235106/