I need to notify users by email, when MyModel
object is created. I need to let them know all attributes of this object including ManyToManyFields.
class MyModel(models.Model):
charfield = CharField(...)
manytomany = ManyToManyField('AnotherModel'....)
def to_email(self):
return self.charfield + '\n' + ','.join(self.manytomany.all())
def notify_users(self):
send_mail_to_all_users(message=self.to_email())
The first thing I tried was to override save function:
def save(self, **kwargs):
created = not bool(self.pk)
super(Dopyt, self).save(**kwargs)
if created:
self.notify_users()
Which doesn't work (manytomany appears to be empty QuerySet
) probably because transaction haven't been commited yet.
So I tried post_save
signal with same result - empty QuerySet
.
I can't use m2mchanged
signal because:
- manytomany can be None
- I need to notify users only if object was created, not when it's modified
Do you know how to solve this? Is there some elegant way?