0

I have a model Location with m2m relation with Place and School

Each time places field is changed I want to set all existing schools to Location

from django.db.models.signals import m2m_changed

class Location(model.Model):

    places = models.ManyToManyField(Place, related_name='locations')

    schools = models.ManyToManyField(School, related_name='locations')


def places_changed(sender, instance, **kwargs):

    instance.schools = School.objects.all()

    print(instance.schools.all())

m2m_changed.connect(places_changed, sender=Location.places.through)

print(instance.schools.all()) - prints all school objects, but nothing saved - db is empty.

Please help) Thx for advices!

MaxCore
  • 2,438
  • 4
  • 25
  • 43
  • try to look here http://stackoverflow.com/questions/38448564/forms-modelform-does-not-save-manytomany-fields/38495003#38495003 – Compadre Jul 27 '16 at 16:10
  • @Compadre It did not help( – MaxCore Jul 27 '16 at 16:31
  • Possible duplicate of [Django: Getting m2m related objects in signals](http://stackoverflow.com/questions/37528922/django-getting-m2m-related-objects-in-signals) – solarissmoke Jul 28 '16 at 04:48

0 Answers0