'Single data from the relation m2m field - Django
In my signal (post_save), I would like to send a single email to a single person who has been assigned using the m2m relationship. One-to-many, the message goes to the user, but m2m nothing happens.
specials = models.ManyToManyField(User, related_name='special_users')
I've tried everything, searched the topics, but still nothing happens. I have included the pseudocode to illustrate the situation.
all_users_from_m2m = instance.specials.all()
for single_user in all_users_from_m2m:
message = ('Subject', 'Here is message', '[email protected]', [single_user.email])
send_mass_mail(message, fail_silently=False)
Solution 1:[1]
I'm not totally sure if I understand your test case but there is a signal called m2m_changed. You could use it like so for instance:
from django.db.models.signals import m2m_changed
def on_some_model_specials_m2m_changed(sender, instance, *args, **kwargs):
# some actions here
pass
m2m_changed.connect(
on_some_model_specials_m2m_changed, sender=models.YourModel.specials.through)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
