Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: save multiple object signal once

Tags:

django

signals

I need some help with sending email when an order is placed. To illustrate the problem, following is the abstract code:

class Order(models.Model):
    user = models.ForeignKey(User)

class OrderItem(modes.Model):
    order = models.ForeignKey(Order, related_name='items')
    item = models.CharField(max_length=255)
    unit_price = models.DecimalField()
    qty = models.IntegerField()
    item_amount = models.DecimalField()

def email_order_on_save(sender, instance, **kwargs):
    # Need order.items.all() here
    pass

post_save.connect(email_order_on_save, sender=Order)

Most of the problems on SO and google seem to deal with one child object at a time; such as this.

Listening to OrderItem would release 5 signals if 5 orders items saved from admin inlines. I can't seem to get my head around this problem. One way, I think (not sure if possible), could be listening to last of all(5) OrderItem's post_save signals.

Any help appreciated.

like image 660
Nav Avatar asked Jul 12 '26 03:07

Nav


2 Answers

I'm guessing you're trying to solve this in the wrong place. Sending an email when the order is completed and saving the Order model are at different levels of abstraction.

I think sending the email should be triggered by some condition in the view that has more information about whether the order is completely saved or not. Think for example of what will happen if an order needs updating (say it's status changes)? Should the email be sent then too?

like image 155
Tom Avatar answered Jul 15 '26 01:07

Tom


Create your own custom signal and send it at the point when you have the data you need saved. Pass in as parameters whatever data structures you need.

Listen for your custom signal in your callback function email_order_on_save and make appropriate decisions based on the parameters about sending or not the e-mail.

like image 43
cethegeek Avatar answered Jul 15 '26 00:07

cethegeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!