﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
19353	Make it easier to extend UserCreationForm for custom user models	Baptiste Mispelon	Berker Peksag	"The documentation [https://docs.djangoproject.com/en/dev/topics/auth/#custom-users-and-the-built-in-auth-forms states] that `UserCreationForm` must be re-written when using a custom user model.

However, for simple subclasses of `AbstractUser` it's actually simpler to extend (rather than rewrite) the existing form like so:
{{{
#!python
class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = CustomUser
}}}

Unfortunately, this fails because the `clean_username` method of `UserCreationForm` still references the original `User` model directly (this technique works with `UserChangeForm` though).
To get it working, the original `clean_username` method needs to be copied over and modified to use the custom user model, like so:
{{{
#!python
class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = CustomUser

    def clean_username(self):
        username = self.cleaned_data[""username""]
        try:
            CustomUser.objects.get(username=username)
        except CustomUser.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])
}}}

This works, but having to copy/paste some code is not a very good practice.

Therefore, I propose to change `UserCreationForm.clean_username` to use `Meta.model` instead of `User`.

This allows the creation of custom user creation forms by simply redefining the user model in the form's `Meta` class (like in the first example).

The documentation could also be extended to show an example of how to extend the builtin auth forms that require it."	New feature	closed	contrib.auth	1.5-alpha-1	Normal	fixed	UserCreationForm AUTH_USER_MODEL	Russell Keith-Magee aenor.realm@… Mjumbe Poe victorhooi@… internet@… flisky maestrofjp berker.peksag@…	Accepted	1	0	0	0	0	0
