为什么Django post_save信号给我pre_save数据? [英] Why does Django post_save signal give me pre_save data?

查看:174
本文介绍了为什么Django post_save信号给我pre_save数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个信息对象连接到许多客户(见下面的代码)



当一个Information对象更新时,我想发送电子邮件每个客户连接到信息。



但是,当我记录信号接收的sold_to字段时,我总是在保存之前获取数据。 p>

我猜这是因为它的ManyToManyField和数据存储在一个单独的表中,但是不应该在所有关系更新后调用post_save信号? p>

任何人都有解决方案的建议?

  class Customer 
name = models.CharField(max_length = 200)
category = models.ManyToManyField('Category',symmetric = False)
contact = models.EmailField()

类信息
name = models.CharField(max_length = 200)
email = models.EmailField(max_length = 200)
mod_date = models.DateTimeField(auto_now = True)
sold_to = models.ManyToManyField(Customer,null = True,blank = True)


def send_admin_email(sender,instance,signal,* args,** kwargs):
from myapp import settings
for cust in instance.sold_to.all():
settings.debug(cust.name)

post_save.connect(send_admin_email,sender =信息)

编辑:#django中的apollo13提醒我:
相关项目这些东西被保存到多对多关系中)
不像您发现的那样保存为模型的保存方法的一部分。 - http://groups.google.com/group/django-users/msg/ 2b734c153537f970



但是从2006年7月9日起,我真的很希望有一个解决方案。

解决方案

您正在面对的问题有一张公开的机票 here 。您可以随时关注它,使其成为发布版本,或者您可以尝试应用它提供的修补程序,并查看是否有帮助。


Im trying to connect a "Information" object to many "Customers" (see code below)

When one Information object is updated, I want to send email to each Customer that is connected to the Information.

However, when I log the sold_to field that the signal recieves I always get what the data is like BEFORE the save.

I'm guessing this is because its ManyToManyField and the data is stored in a separate table, but shouldn't the post_save signal be called after all relations have been updated?

Anyone got a suggestion for a solution?

class Customer
    name = models.CharField(max_length=200)
    category = models.ManyToManyField('Category',symmetrical=False)
    contact = models.EmailField()

class Information
    name = models.CharField(max_length=200)
    email = models.EmailField(max_length=200)
    mod_date = models.DateTimeField(auto_now=True)
    sold_to = models.ManyToManyField(Customer, null=True, blank=True)


def send_admin_email(sender, instance, signal, *args, **kwargs):
    from myapp import settings
    for cust in instance.sold_to.all():
        settings.debug(cust.name)

post_save.connect(send_admin_email, sender=Information)

Edit: apollo13 in #django alerted me to this: "Related items (the things being saved into the many-to-many relation) are not saved as part of a model's save method, as you have discovered." - http://groups.google.com/group/django-users/msg/2b734c153537f970

But since its from Jul 9 2006 I really really hope there is a solution for this.

解决方案

There's an open ticket for the issue you are facing here. You could either keep an eye on that for when it makes it into a release, or you could try applying the patch that it provides and see if that helps.

这篇关于为什么Django post_save信号给我pre_save数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆