#28608: Allow UserCreationForm and UserChangeForm to work with custom user 
models
-------------------------------------+-------------------------------------
     Reporter:  Rômulo Collopy       |                    Owner:
         Type:                       |  shangdahao
  Cleanup/optimization               |                   Status:  new
    Component:  contrib.auth         |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  user, custom user,   |             Triage Stage:  Accepted
  auth                               |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Lemuel Formacil):

 Another non-standard behavior of the `UserCreationForm` when used with a
 custom user model is if the model has a `ManyToManyField` the form will
 not save the value of the `ManyToManyField`.  From the
 [[https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-
 method|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.

 However, the `UserCreationForm.save` method initially calls the form's
 save method with `commit=False` but then doesn't call the `save_m2m`
 method within the `if commit:` block.  The save method should be something
 like this so the form would behave as expected with custom user models
 with a many-to-many relation:

 {{{
 def save(self, commit=True):
     user = super().save(commit=False)
     user.set_password(self.cleaned_data["password1"])
     if commit:
         user.save()
         self.save_m2m()  # added this call to work with models with
 `ManyToManyField`s
     return user
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/071.328076a33d660dda803f69f724b21ea3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to