and
{{{
class UserCreationForm(forms.ModelForm):
class Meta:
model = User
}}}
could use `UserModel` instead of `User`. It is already defined in
https://github.com/django/django/blob/01c6a3e227b645e8dea97e9befecd23d1d3b8581/django/contrib/auth/forms.py#L20
and is used in other forms of the same package.
This would allow Django Registration and other packages that use these
forms to work out of the box. And it would allow using them with no need
to rewitte as specified at
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#custom-
users-and-the-built-in-auth-forms
--
Ticket URL: <https://code.djangoproject.com/ticket/28608>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "patch.diff" added.
patch
* Attachment "patch.diff" added.
patch
--
* needs_better_patch: 0 => 1
Comment:
This is a continuation of #19353.
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:2>
* status: new => assigned
* needs_better_patch: 1 => 0
* owner: nobody => hui shang
Comment:
Since This ticket is related to
[https://code.djangoproject.com/ticket/28757 ticket 28757], I made the
changes for this ticket and ticket 28757 in the same commit.
[https://github.com/django/django/pull/9354 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:3>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"3333d935d2914cd80cf31f4803821ad5c0e2a51d" 3333d93]:
{{{
#!CommitTicketReference repository=""
revision="3333d935d2914cd80cf31f4803821ad5c0e2a51d"
Fixed #28757 -- Allowed using contrib.auth forms without installing
contrib.auth.
Also fixed #28608 -- Allowed UserCreationForm and UserChangeForm to
work with custom user models.
Thanks Sagar Chalise and Rômulo Collopy for reports, and Tim Graham
and Tim Martin for reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:4>
* status: closed => new
* has_patch: 1 => 0
* resolution: fixed =>
Comment:
In f3fa86a89b3b85242f49b2b9acf58b5ea35acc1f:
Fixed #29449 -- Reverted "Fixed #28757 -- Allowed using contrib.auth forms
without installing contrib.auth."
This reverts commit 3333d935d2914cd80cf31f4803821ad5c0e2a51d due to a
crash if USERNAME_FIELD isn't a CharField.
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:5>
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>
* cc: Lemuel Formacil (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:7>
* owner: shangdahao => Aman Pandey
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28608#comment:8>