Answer the question
In order to leave comments, you need to log in
Django: Changing ManyToManyField in save() method?
When creating a model, you need to analyze the contents of the fields and make changes to the ManyToManyField.
If save is called directly (in the view or from another model) - no problem. But when called from admin, the result of the save operation will be replaced by the result of the form processing.
I suspect that editing the ManyToManyField in .save() is not in line with the django concept. Please advise how to implement it correctly.
Answer the question
In order to leave comments, you need to log in
There is no "form processing result", any ModelForm always calls the model's .save method. And it doesn't matter if it's in the admin panel, or somewhere else.
The problem is different, and the .save method code would not hurt here.
A similar problem can be when overriding save in ModelForm, you need to explicitly call the .save_m2m method there, but this is a finger in the sky, you need to see the code.
I don't see how editing a ManyToManyField in .save is against the Django concept, I think it's perfectly normal (albeit a little strange).
To be honest, I delved into the issue for a long time, when saving the form, .save () is still called on the model. As a way to solve the problem, look towards pre_save, post_save
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question