[Answer]-Many to many field in django is working in admin site but not in front end site form

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 a save_m2m() method to your ModelForm subclass. After you’ve manually saved the instance produced by the form, you can invoke save_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

Leave a comment