Methods related with m2m fields (clear, set) doesn’t works
in model save() method with using from admin panel.
Bug not happened, when instance creating or
updating was initiated from other sources (shell, api of management command)
Details (testcase with instructions to reproduction) here:
Is that a bug or it is works as planned?
This is working as designed. And, in fact, the relationship is being cleared, but then being populated.
When the admin is saving related fields, it first saves the base model (calling the base model save
method), and then saves the relationships.
So what’s happening here is that the relationship is being cleared and then being repopulated.
(You can see this by using the --print-sql
parameter on the runserver_plus
command in the django-extensions
package.)
If you need to do something with the relationships in the admin, the ModelAdmin.save_related
method appears to be the most appropriate hook for you to use.
Ken