mysql - Django DatetimeField 在更新时发生更改,值不正确

标签 mysql django datetime

我有一个这样的模型:

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/

相关文章:

mysql - 如何在mysql中查询两个日期,其中datefrom位于同一表的一列中,dateto位于同一表的第二列中?

mysql - 将 mysql 连接到 postgresql

python - 用于外部 API 调用的可浏览 Django REST API 包装器

python - 使用 WSGIAuthUserScript 时如何设置 python 路径?

Javascript:以 YYYY/mm/dd hh:m:sec 格式输出当前日期时间

php - 为什么 PHP 将某些 float 解析为奇怪的日期?

c# - 可以选择但不能插入数据库 mysql C# ASP.NET

mysql - 查询1行2条记录

django - 在 GenericForeignKey 中实现模型限制

c# - 从 Kinect 传感器数据计算 BPM