Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含几列的 django 模型,这两个: - is_completed (boolean) - completed_datetime (datetime)
现在我想在 is_completed 更改时自动更新日期时间字段。有一个简单的方法吗?Auto_now 不起作用,因为我不想在更新 is_completed 以外的其他字段时更新 datetime 字段。
覆盖模型保存方法:
from django.utils import timezone from django.db import models class MyModel(models.Model): def save(self, *args, **kw): if self.is_completed: self.completed_datetime = timezone.now() super(MyModel, self).save(*args, **kw)