1👍
✅
You have to call save_m2m()
:
cnf = create_notice_form.save(commit=False)
cnf.created_by = request.user
cnf.save()
create_notice_form.save_m2m()
Excerpt from the documentation:
If your model has a many-to-many relation and you specify
commit=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.
0👍
You have to Call the following after validating the form:-
if create_notice_form.is_valid():
parent = create_notice_form.save(commit=False)
parent.save()
create_notice_form.save_m2m()
I hope this will help you
- [Answer]-Displaying youtube video in django 1.8 by passing id as CharField
- [Answer]-How to have next and previous links on a webpage in django:
- [Answer]-Django template rendering extend tag incorrectly
- [Answer]-Acces html form fields inside django view with request objects
- [Answer]-Migration problems after update Django 1.8.1
Source:stackexchange.com