Calling save_m2m() in

Skip to first unread message

Mark Gensler

Nov 23, 2022, 12:18:14 PM11/23/22
to Django developers (Contributions to Django itself)
Hello all!

I noticed that when calling, self.save_m2m() is not called when commit=True.

This caused an issue in one of my projects which uses a custom User model with ManyToMany fields. Is this something that should be changed? I saw in the docs [1] that it is advised to extend UserCreationForm and UserChangeForm if using a custom User model.

A few reasons I see to add this step to the method:
  • UserCreationForm is a subclass of ModelForm, which does call save_m2m() when commit=True.
  • UserChangeForm does call save_m2m() as part of save(), because the save() method is not overloaded. This seems inconsistent!
The solution I'd propose is:

class UserCreationForm(forms.ModelForm):
    def save(self, commit=True):
        user = super().save(commit=False)
        if commit:
            if hasattr(self, "save_m2m"):
        return user

I'd be happy to raise a ticket and work on a patch if this change would be useful.


Adam Johnson

Nov 27, 2022, 5:47:01 AM11/27/22
Your proposal seems reasonable - if actually saving, we should save the m2m fields too.

I think the best next step would be to file a ticket and work on a PR. The first step would be to add a test case reproducing the issue.

You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Mark Gensler

Nov 27, 2022, 9:47:50 AM11/27/22
to Django developers (Contributions to Django itself)
Hi Adam, thanks for the reply. I'll open a ticket and start work on a PR. Mark
Reply all
Reply to author
0 new messages