更新父模型w / Django中的auto_now DateTimeField [英] updating an auto_now DateTimeField in a parent model w/ Django

查看:133
本文介绍了更新父模型w / Django中的auto_now DateTimeField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:消息和附件。每个附件都附加到特定邮件,使用附件模型上的ForeignKey。两个模型都有一个auto_now DateTimeField,名为updated。我试图使它,以便当任何附件被保存时,它还设置相关消息上的更新字段到现在。这是我的代码:

I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message to now. Here's my code:

def save(self):
    super(Attachment, self).save()
    self.message.updated = self.updated

这将工作,如果你可以解释对我来说,为什么?如果没有,我该如何做到这一点?

Will this work, and if you can explain it to me, why? If not, how would I accomplish this?

推荐答案

您还需要保存消息。那么它应该工作。

You would also need to then save the message. Then it that should work.

这篇关于更新父模型w / Django中的auto_now DateTimeField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆