且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在admin中内联编辑时Django触发父模型保存

更新时间:2023-11-30 16:01:22

您有一些解决方案。这样,从简单到复杂:

You have a few solutions. Here goes, from simpler to more complex:

您可以为 save 方法> ChildModel 调用 ParentModel.save

您还可以连接到 ChildModel post_save pre_save 信号。

You could implement a custom save method for ChildModel that calls ParentModel.save.
You could also connect to your ChildModel's post_save or pre_save signal.

现在,如果您要一次更新很多 ChildModel 实例,这两个解决方案将变得很烦人,因为您将调用 ParentModel.save 多次,可能没有目的。

然后,您可能想使用以下内容:

覆盖您的 ParentModel ModelAdmin.change_view 处理您的逻辑;但是,这非常棘手。

Now, these two solutions will prove annoying if you're going to update a lot of ChildModel instances at once, as you will be calling ParentModel.save several times, maybe without purpose.
You might then want to use the following:
Override your ParentModel's ModelAdmin.change_view to handle your logic; this is pretty tricky however.

我对您遇到的行为感到非常惊讶,从检查源代码开始,无论如何应该保存对象;是否编辑。

I'm however pretty surprised by the behavior your're encountering, from checking the source, the object should be saved anyway; edited or not.