[Django] #26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value

12 views
Skip to first unread message

Django

unread,
Mar 20, 2016, 5:48:10 AM3/20/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+--------------------
Reporter: berkerpeksag | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------
I noticed this while working on #25617. If you have a custom user model
with `USERNAME_FIELD = 'email'`, you can't just subclass
`UserCreationForm`.

Here is a test case:

{{{
diff --git a/tests/auth_tests/test_forms.py
b/tests/auth_tests/test_forms.py
index 6172c44..1d4cd48 100644
--- a/tests/auth_tests/test_forms.py
+++ b/tests/auth_tests/test_forms.py
@@ -20,7 +20,7 @@ from django.utils.encoding import force_text
from django.utils.text import capfirst
from django.utils.translation import ugettext as _

-from .models.custom_user import ExtensionUser
+from .models.custom_user import CustomUser, ExtensionUser
from .settings import AUTH_TEMPLATES


@@ -139,6 +139,22 @@ class UserCreationFormTest(TestDataMixin, TestCase):
form = CustomUserCreationForm(data)
self.assertTrue(form.is_valid())

+
@override_settings(AUTH_MODEL='auth_tests.models.custom_user.CustomUser')
+ def test_custom_form_with_different_username_field(self):
+ class CustomUserCreationForm(UserCreationForm):
+ class Meta(UserCreationForm.Meta):
+ model = CustomUser
+ fields = ('email', 'date_of_birth')
+
+ data = {
+ 'email': 'te...@client222.com',
+ 'password1': 'testclient',
+ 'password2': 'testclient',
+ 'date_of_birth': '1988-02-24',
+ }
+ form = CustomUserCreationForm(data)
+ self.assertTrue(form.is_valid())
+
def test_password_whitespace_not_stripped(self):
data = {
'username': 'testuser',
}}}

And here is the traceback:

{{{
Traceback (most recent call last):
File "/home/berker/projects/django/django/test/utils.py", line 208, in
inner
return func(*args, **kwargs)
File "/home/berker/projects/django/tests/auth_tests/test_forms.py", line
155, in test_custom_form_with_different_username_field
form = CustomUserCreationForm(data)
File "/home/berker/projects/django/django/contrib/auth/forms.py", line
85, in __init__
self.fields['username'].widget.attrs.update({'autofocus': ''})
KeyError: u'username'
}}}

I will open a oull request to fix this shortly.

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

Django

unread,
Mar 20, 2016, 5:48:55 AM3/20/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+--------------------------------------

Reporter: berkerpeksag | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by berkerpeksag):

* cc: berker.peksag@… (added)
* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26381#comment:1>

Django

unread,
Mar 20, 2016, 6:26:33 AM3/20/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+--------------------------------------

Reporter: berkerpeksag | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by berkerpeksag):

* has_patch: 0 => 1


Comment:

Pull request: https://github.com/django/django/pull/6315

--
Ticket URL: <https://code.djangoproject.com/ticket/26381#comment:2>

Django

unread,
Mar 20, 2016, 6:27:03 AM3/20/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+----------------------------------------
Reporter: berkerpeksag | Owner: berkerpeksag
Type: Bug | Status: assigned
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by berkerpeksag):

* owner: nobody => berkerpeksag
* status: new => assigned


Old description:

New description:

I will open a pull request to fix this shortly.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26381#comment:3>

Django

unread,
Mar 21, 2016, 8:33:32 AM3/21/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+----------------------------------------
Reporter: berkerpeksag | Owner: berkerpeksag
Type: Bug | Status: assigned
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by timgraham):

* version: 1.9 => master
* stage: Unreviewed => Accepted


Comment:

The line that needs to be fixed is new in Django 1.10, so this doesn't
affect 1.9.

--
Ticket URL: <https://code.djangoproject.com/ticket/26381#comment:4>

Django

unread,
Mar 21, 2016, 12:37:28 PM3/21/16
to django-...@googlegroups.com
#26381: UserCreationForm cannot be reused with a custom USERNAME_FIELD value
------------------------------+----------------------------------------
Reporter: berkerpeksag | Owner: berkerpeksag
Type: Bug | Status: closed
Component: contrib.auth | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"efa9539787dbdd06cd2169023edcf7db3e2ff0c4" efa9539]:
{{{
#!CommitTicketReference repository=""
revision="efa9539787dbdd06cd2169023edcf7db3e2ff0c4"
Fixed #26381 -- Made UserCreationForm reusable with custom user models
that define USERNAME_FIELD.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26381#comment:5>

Reply all
Reply to author
Forward
0 new messages