简体   繁体   English

为什么Django post_save信号给我pre_save数据?

[英]Why does Django post_save signal give me pre_save data?

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. 当一个Information对象更新时,我想向连接到Information的每个Customer发送电子邮件。

However, when I log the sold_to field that the signal recieves I always get what the data is like BEFORE the save. 但是,当我记录信号收到的sold_to字段时,我总是得到保存之前的数据。

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

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." 编辑:#django中的apollo13提醒我:“相关项目(被保存为多对多关系的东西)不会像你发现的那样保存为模型的保存方法的一部分。” - http://groups.google.com/group/django-users/msg/2b734c153537f970 - 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. 但自2006年7月9日起,我真的希望有一个解决方案。

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. 您可以密切关注它何时进入发行版,或者您可以尝试应用它提供的补丁,看看是否有帮助。

This is my solution, after applying the patch from code.djangoproject.com mentioned above. 这是我的解决方案,在应用上面提到的code.djangoproject.com中的补丁之后。

Added this in models.py: 在models.py中添加了这个:

from django.db.models.signals import m2m_changed
m2m_changed.connect(send_admin_email, sender=Information)

And the send_admin_email function: 而send_admin_email函数:

def send_customer_email(sender, instance, action, model, field_name, reverse, objects, **kwargs):
    if ("add" == action):
        # do stuff

I run across the same issue since I have M2M fields in my model I also got the pre_save like data. 我遇到了同样的问题,因为我的模型中有M2M字段,我也得到了pre_save数据。

In the situation, the problem is that in M2M fields both related models should be saved in order to get the auto generated IDs. 在这种情况下,问题是在M2M字段中应该保存两个相关模型以获得自动生成的ID。

In my solution, neither I used the post_save signal nor m2m_changed signal, instead of that signals I used log_addition and log_change methods in ModelAdmin class definition. 在我的解决方案中,我既没有使用post_save信号也没有使用m2m_changed信号,而是使用了ModelAdmin类定义中的log_addition和log_change方法。

In your custom ModelAdmin class: 在您的自定义ModelAdmin类中:

    class CustomModelAdmin(admin.ModelAdmin):
         def log_addition(self, request, object):
         """
         Log that an object has been successfully added.
         """
             super(CustomModelAdmin, self).log_addition(request, object)
             #call post_save callback here object created

         def log_change(self, request, object):
         """
         Log that an object has been successfully changed.
         """
             super(CustomModelAdmin, self).log_change(request, object)
             #call post_save callback here object changed

If you want, you may also override log_deletion() method. 如果需要,您还可以覆盖log_deletion()方法。

Happy overriding... 快乐压倒一切......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM