内联模型形式发出post_save信号? (django的) [英] Do inline model forms emmit post_save signals? (django)
问题描述
我想要的是在更改第二个模型时执行一些其他操作。我可以用ModelTwo上的post_save信号来做到这一点。但是,当我将模型从自己的编辑页面(即不在ModelOne的内联中)保存下来时,只会调用post_save信号。
有没有办法附加poststave信号在ModelTwo的内联格式?
...作为一种解决方法,我向ModelTwo添加了一些自定义验证,无论内联是否被调用,都被调用),调用我想要的方法然而,由此设置引起的问题是,如果正在创建一个ModelOne的新实例,并且同时创建了ModelTwo的实例,则无法访问关联两个表的新实例的主键(外键)(因为它还没有保存)。而且主键是我需要的东西。
我还尝试直接向ModelOne添加一个post_save信号(以便我可以获得新的实例的PK),但是似乎post_save信号没有通过ModelTwo的数据(为什么还要呢?)
那么这个解决方案是什么?内联模型是否发射信号?在存储新实例之前有没有办法访问新实例的PK?
模型是模型。模型在管理界面中作为内联使用的事实不会以任何方式从其中消失。除非您重写其功能,否则所有型号均会发出后保存信号。
So I have two models (Tables) related by a ForeignKey. In the admin, the edit page displays the first model (let's say ModelOne) along with the related instances of the second model, ModelTwo (TabularInline).
What I want is perform some additional actions when the second model is being changed. I can do this with a post_save signal on ModelTwo. However, the post_save signal is only called when I save the model from within its own edit page (ie. not within ModelOne's inlines).
Is there a way to attach a post_save signal on ModelTwo's inline form?
...As a workaround I added some custom validation to ModelTwo, which is being called regardless if it's inline or not), to call the method I want. However, the problem that arises from this setting is that if am creating a new instance of ModelOne and also create instances of ModelTwo at the same time I cannot access the primary key (foreign key) of the new instance that relates the two tables (since it has not been saved yet). And the primary key is something I need.
I also tried adding a post_save signal to ModelOne directly (so that I can get the new instance's PK) but it seems that the post_save signal does not pass ModelTwo's data (and why should it, anyway?)
So what's the solution to this? Do inline models emit signals? Is there a way to access the PK of the new instance before saving it?
Models are Models. The fact that a Model is used in the admin interface as inline doesn't take away from it in any way. All models emit a post save signal unless you override its functionality.
Here is what happens when you save any model.
这篇关于内联模型形式发出post_save信号? (django的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!