2👍
The docs you linked to explain this:
Calling save_m2m() is only required if you use save(commit=False). When you use a simple save() on a form, all data – including many-to-many data – is saved without the need for any additional method calls.
Also, it looks like event
is itself a model instance rather than a form. save_m2m
is required for forms, not model instances.
To paraphrase the explanation in the docs: a form’s save
method, if called with commit=True
(the default) does two things – it creates a new model instance using the form’s cleaned data, and it writes that model instance to the database. Then, if there are any many-to-many relationships, it writes those to the database as well. It does that after writing the instance because you can’t write a many-to-many relationship until the instance has a primary key, which is auto-generated when you write it to the database.
If you call the form’s save
method with commit=False
, it creates the new model instance but it does not write it to the database. Thus, it has no primary key yet and the many-to-many information can’t be saved either. After you save the model instance and therefore generate a primary key for it, the many-to-many information is still stored only in the form object. So you need to notify the form object that it’s now OK to save the many-to-many information, by calling save_m2m
.
edit Since you’ve added more of your code I can see more clearly what you’re asking. The above code will not save many-to-many relationships that are set in your EventForm
instance. Is that what your loop to set opportunities is doing?