M
M
meteozond2011-04-15 10:32:10
Django
meteozond, 2011-04-15 10:32:10

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

3 answer(s)
S
savados, 2011-04-15
@meteozond

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).

V
Vlad Frolov, 2011-04-15
@frol

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

A
adsinger, 2011-04-15
@adsinger

As an option, set manytomany through through and overload the save in the bundle object, for the data, request the model itself through the related manager.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question