Skip to content

Commit

Permalink
[1.10.x] Fixed #27111 -- Fixed KeyError if USERNAME_FIELD isn't in Us…
Browse files Browse the repository at this point in the history
…erCreationForm.fields.

Backport of 3c18f8a from master
  • Loading branch information
berkerpeksag authored and timgraham committed Aug 24, 2016
1 parent 6f006f4 commit c4ee931
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django/contrib/auth/forms.py
Expand Up @@ -94,7 +94,8 @@ class Meta:

def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': ''})
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': ''})

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.10.1.txt
Expand Up @@ -76,3 +76,6 @@ Bugfixes
* Included the already applied migration state changes in the ``Apps`` instance
provided to the ``pre_migrate`` signal receivers to allow ``ContentType``
renaming to be performed on model rename (:ticket:`27100`).

* Reallowed subclassing ``UserCreationForm`` without ``USERNAME_FIELD`` in
``Meta.fields`` (:ticket:`27111`).
18 changes: 17 additions & 1 deletion tests/auth_tests/test_forms.py
Expand Up @@ -22,7 +22,9 @@
from django.utils.text import capfirst
from django.utils.translation import ugettext as _

from .models.custom_user import CustomUser, ExtensionUser
from .models.custom_user import (
CustomUser, CustomUserWithoutIsActiveField, ExtensionUser,
)
from .settings import AUTH_TEMPLATES


Expand Down Expand Up @@ -208,6 +210,20 @@ class Meta(UserCreationForm.Meta):
form = CustomUserCreationForm(data)
self.assertTrue(form.is_valid())

def test_custom_form_hidden_username_field(self):
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUserWithoutIsActiveField
fields = ('email',) # without USERNAME_FIELD

data = {
'email': 'testclient@example.com',
'password1': 'testclient',
'password2': 'testclient',
}
form = CustomUserCreationForm(data)
self.assertTrue(form.is_valid())

def test_password_whitespace_not_stripped(self):
data = {
'username': 'testuser',
Expand Down

0 comments on commit c4ee931

Please sign in to comment.