Django: save multiple object signal once

…衆ロ難τιáo~ 提交于 2019-12-04 16:39:59

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.

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?

You could create your model as follows

ORDER_STATE = (
    (1, 'Completed'),
    (2, 'Processing'),
)

class Order(models.Model):
    user = models.ForeignKey(User)
    state = models.IntegerField(choices = ORDER_STATE)

You could have many states for the order. The state "Completed" could represent that the order processing is complete. You could change the state of your order in your views.

In the signal handler, you could check for the state of the order and then send mail, if the order is in completed state.

diegueus9

I think you can have a problem with signals, OrderItem with inlines will not send save signal, read this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!