django post_save信号发送过时的内联表单集
请考虑以下事项:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本问题是,当发送主对象
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 onModelAdmin
is your hook.