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

发布于 2024-09-30 17:08:54 字数 1381 浏览 3 评论 0原文

请考虑以下事项:

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(...)

现在,每当有人创建或修改订单时,我想发送一封包含订单详细信息的电子邮件。

到目前为止还没有什么火箭科学……让我们只使用 post_save 信号;

post_save.connect(email_notify, sender=OrderForm)

但有一个小问题,传递给 email_notify 的 OrderForm 对象会按预期使用新数据进行更新,但相关的 OrderFormLine 项目不会更新。

我试图覆盖管理和模型中的保存方法,我试图在将对象、表单及其关系传递给我的通知处理程序之前保存它,但没有任何效果。

我知道我可以将 post_save 信号附加到 OrderItem 模型,但随后将为每个项目发送电子邮件。

救救我,我正处于疯狂的边缘。

更新

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

简短的故事:

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)

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.

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

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.

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.

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-10-07 17:08:55

基本问题是,当发送主对象 post_save 信号时,内联尚未保存:父模型始终首先保存。所以,这并不是说它正在发送旧数据;而是它正在发送旧数据。事实上,这是数据的当前状态。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文