2
Use save_m2m()
. From the docs:
Another side effect of using
commit=False
is seen when your model has a many-to-many relation with another model. If your model has a many-to-many relation and you specifycommit=False
when you save a form, Django cannot immediately save the form data for the many-to-many relation. This is because it isn’t possible to save many-to-many data for an instance until the instance exists in the database.To work around this problem, every time you save a form using
commit=False
, Django adds asave_m2m()
method to yourModelForm
subclass. After you’ve manually saved the instance produced by the form, you can invokesave_m2m()
to save the many-to-many form data.
Source:stackexchange.com