[Fixed]-Django clear ManyToManyField associations on update

1👍

You need to use clear() method not remove(). According to docs, delete() removes a specified model objects from the related object set

>>> b = Blog.objects.get(id=1)
>>> e = Entry.objects.get(id=234)
>>> b.entry_set.remove(e) # Disassociates Entry e from Blog b.

But, that is not your case, you want to disassociate all of them not a specified one.

 def save(self, *args, **kwargs):
      # after figuring out "school_id" changed
      self.students.clear()

      super(Teacher, self).save(*args, **kwargs)
👤levi

Leave a comment