Search code examples
djangodjango-modelsdjango-signals

Error getting ManyToMany field from an object


How to execute some functionality, after creating an object in the admin area? I'm trying to use post_save signal and trying to get all objects from my field, which has type ManyToMany, I also use sorting package (sortedm2m). When I save the object I try to output this field, but when I create I get an empty queryset, and when I edit I get the old queryset, without the current changes.

class Servers(models.Model):
    name = models.CharField(max_length=120, default="name")
    content = SortedManyToManyField(Content)


@receiver(post_save, sender=Servers)
def create_server(sender, instance, **kwargs):
    print(instance.content.all())

Solution

  • You have to use m2m_changed

    Otherwise you can not be able to catch manytomany fields in signal.