我有一个模型位置,m2m与地点和学校
的关系每次地点字段更改时,我想将所有现有的学校设置为位置
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()) - 打印所有学校对象,但没有保存 - db为空。
请帮助)Thx for advice!