سؤال

I want to send out an email when a model instance is saved. To do this I listen out for the post_save signal:

#models.py
@receiver(post_save, sender=MyModel, dispatch_uid="something")
def send_email(sender, **kwargs):
    instance = kwargs['instance']
    email = ModelsEmailMessage(instance)  
    email.send()

In my view I process the form and append subscribers to the object:

#views.py
object = form.save()
object.subscribers.add(*users)

My problem is that the form save triggers the post_save signal before the users have been added.

But if I do this:

object = form.save(commit=False)

then I can't add m2m instances to an object that has no id.

Heyulp!

هل كانت مفيدة؟

المحلول

Most likely you will have to write your own signal to send email.

Event though you are implemented that tries to send email when object is saved, but that is not what you want. You want to send email when object is saved and has some subscribers added after processing some view. i.e. its 2 step operation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top