django post_save信号发送过时的内联表单集

时间:2022-11-15 20:04:03

Consider the following:

考虑以下:

class OrderForm(models.Model):
    title = models.CharField(max_length=100)
    desc  = models.TextField()


class OrderFormLine(models.Model):
    order = models.ForeignKey(OrderForm)
    lagel = models.CharField(max_length=100)
    qty   = models.IntegerField(...)
    price = models.FloatField(...)

Now I want to send an email with the orderform details whenever someone creates one or modify one.

现在我想要发送一封包含orderform详细信息的电子邮件,无论何时有人创建一个或修改一个。

No rocket science so far .. let's just use a post_save signal;

到目前为止没有火箭科学。我们使用一个post_save信号;

post_save.connect(email_notify, sender=OrderForm)

But there's one tiny problem, the OrderForm object passed to email_notify is updated with the new data as expected, but not the related OrderFormLine items.

但是有一个小问题,传递给email_notify的OrderForm对象按照预期的新数据更新,而不是相关的OrderFormLine项。

I've tried to override the save methods in the admin AND in the model, I've tried to save the object, the form and its relation before passing it to my notification handler, nothing works.

我尝试在管理和模型中覆盖save方法,我尝试在将对象、表单和它的关系传递给通知处理程序之前保存它,什么都不起作用。

I'm aware that I could attach the post_save signal to the OrderItem model, but then the email would be sent for each items.

我知道我可以将post_save信号附加到OrderItem模型,但是邮件将被发送到每个项目。

Help I'm on the brink of madness.

我快疯了。

UPDATE:

更新:

Found a simple and reliable solution

找到一个简单可靠的解决方案

Short story:

短篇小说:

def email_notify_orderform(sender, **kwargs):
    instance = kwargs['instance']
    ct = ContentType.objects.get_for_model(OrderForm)
    if ct.id == instance.content_type.id:
        print instance.is_addition()
        print instance.is_change()
        print instance.is_deletion()
        print instance.change_message
        print instance.action_time
        print instance.get_edited_object().total() # BINGO !
post_save.connect(email_notify_orderform, sender=LogEntry)

1 个解决方案

#1


6  

The basic problem is that when the main objects post_save signal is sent, the inlines have not been saved yet: the parent model always gets saved first. So, it's not that it's sending old data; in fact it's the current state of the data.

基本的问题是,当发送主对象post_save信号时,inline还没有被保存:父模型总是首先被保存。所以,它不是发送旧数据;事实上,它是数据的当前状态。

The simplest solution is to create a custom signal and have that signal sent at a place where the inlines have been saved. The save_formset method on ModelAdmin is your hook.

最简单的解决方案是创建一个自定义信号,并将该信号发送到保存inlines的位置。ModelAdmin上的save_formset方法是您的挂钩。

#1


6  

The basic problem is that when the main objects post_save signal is sent, the inlines have not been saved yet: the parent model always gets saved first. So, it's not that it's sending old data; in fact it's the current state of the data.

基本的问题是,当发送主对象post_save信号时,inline还没有被保存:父模型总是首先被保存。所以,它不是发送旧数据;事实上,它是数据的当前状态。

The simplest solution is to create a custom signal and have that signal sent at a place where the inlines have been saved. The save_formset method on ModelAdmin is your hook.

最简单的解决方案是创建一个自定义信号,并将该信号发送到保存inlines的位置。ModelAdmin上的save_formset方法是您的挂钩。