auto_now_add=Trueとauto_now=True仮定しeditable=Falseます。そのため、管理者または他の でこのフィールドを変更する必要がある場合は、設定ModelFormを使用しないでください。auto_now_*=True
これらのフィールドの自動更新はauto_now_*、Django レベルで処理されます。
モデルのインスタンスをauto_now_*=Trueフィールドで更新すると、Django は自動的にフィールドを更新します。
class Article(models.Model):
active = models.BooleanField()
updated = models.DateTimeField(auto_now=True)
article = Article.object.get(pk=10)
article.active = True
article.save()
# ASSERT: article.updated has been automatically updated with the current date and time
Django でこの自動動作をオーバーライドしたい場合は、queryset.update() を介してインスタンスを更新することで実行できます。
Article.object.filter(pk=10).update(active=True)
# ASSERT: Article.object.get(pk=10).updated is unchanged
import datetime
Article.object.filter(pk=10).update(updated=datetime.datetime(year=2014, month=3, day=21))
# ASSERT: article.updated == March 21, 2014