0

I have a following model and function signal_task which just outputs the test_users field objects on signal post_save

class Test_Model(models.Model):
    test_users = models.ManyToManyField(User)

    def signal_task(self):
        print(self.test_users.all())
@receiver(post_save,sender=Test_Model)
def Test_signal(sender,instance=None,created=False,**kwargs):
    if created:
        instance.signal_task()
  • On Creating Test_Model objects, singal_task() executes which should output the User objects assigned to test_users, But it outputs the empty queryset <QuerySet []> , How do i output the test_users value?
Arbazz Hussain
  • 1,622
  • 2
  • 15
  • 41
  • 4
    By that time the `ManyToManyField` does not contain any elements. Django will first create the object (triggering `pre_save` and `post_save`), and *then* add elements to the m2m relation. It can not do that differently since as long as it is not saved, it does not have a primary key for the "through" table. – Willem Van Onsem Dec 16 '19 at 14:25
  • You can use `m2m_changed`: https://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed – Navid Zarepak Dec 16 '19 at 14:38

0 Answers0