Django admin – 允许手动编辑自动DateTime字段

是否可以在模型的添加/更改页面上手动编辑自动DateTimeField.字段定义为:

post_date = models.DateTimeField(auto_now_add=True)
post_updated = models.DateTimeField(auto_now=True)

我不确定如何手动覆盖这些将完全起作用,是在数据库级别还是在django本身处理自动更新?

最佳答案 auto_now_add = True且auto_now = True
assume editable = False.因此,如果您需要更正此字段,请不要使用它们.

在django级别自动更新句柄.例如,如果您更新查询集,例如

Article.object.filter(pk=10).update(active=True)

不会更新post_updated字段.但

article = Article.object.get(pk=10)
article.active = True
atricle.save()

会做

点赞