我使用下面的命令从我的 MySQL 数据库自动生成模型。
python manage.py inspectdb > models.py
对于某些表,我有insert_time 和update_time 字段,在转换为模型后,它们变成了DateTimeField 类型,我发现这两个字段不会更新到数据库。我需要为 insert_time 字段添加 auto_now_add=True
并为 update_time 字段手动添加 auto_now=True
...是有没有办法为 inspectdb 或任何其他更有效的方法提供配置?谢谢。
最佳答案
Django 无法通过检查数据库判断日期时间字段是否应该具有 auto_now_add = True
或 auto_now = True
。这是您在运行 inspectdb
后需要自己修复的事情。
来自 the docs :
This feature is meant as a shortcut, not as definitive model generation. After you run it, you’ll want to look over the generated models yourself to make customizations.
如果您有很多字段要更新,您的文本编辑器可能有一个功能可以让您更快地更新字段,例如搜索和替换或宏。
关于python - DateTimeField 的 django inspectdb 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776738/