【发布时间】:2014-04-09 18:15:02
【问题描述】:
我有一个模型,我们称之为Post。我有另一个模型,我们称之为Comment。一条评论可以有很多帖子。换句话说,它看起来有点像这样:
class Comment(models.Model):
post = models.ForeignKey('app.Post', blank=True, null=True)
我的 Post 模型有一个 last_modified 字段,如下所示
class Post(models.Model):
last_modified = models.DateTimeField(auto_now=True)
当我直接更改Post 时,最后修改的时间戳很容易得到更新。但是,我还需要 last_modified 来反映模型的 comment_set 何时更改——换句话说,何时添加了 Comment。
我想将此逻辑包含在Post 模型本身中,而不是将其添加到评论的保存方法或类似的东西中,这样我就可以使模型尽可能干净。是否有任何功能类似于 Django 的 m2m_changed 模型,允许我在 Post 的评论集发生更改时更新 last_modified ?
【问题讨论】:
-
根据您的模型,一个
post可以有多个comments。不然。