Skip to content

Commit 6ea9025

Browse files
committed
[1.2.X] Fixed #14242 - UserChangeForm subclasses without 'user_permissions' field causes KeyError
This was a regression introduced by [13684] Thanks to [email protected] for report. Backport of [13702] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13704 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 5357000 commit 6ea9025

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

django/contrib/auth/forms.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class Meta:
5454

5555
def __init__(self, *args, **kwargs):
5656
super(UserChangeForm, self).__init__(*args, **kwargs)
57-
self.fields['user_permissions'].queryset = self.fields['user_permissions'].queryset.select_related('content_type')
57+
f = self.fields.get('user_permissions', None)
58+
if f is not None:
59+
f.queryset = f.queryset.select_related('content_type')
5860

5961
class AuthenticationForm(forms.Form):
6062
"""

django/contrib/auth/tests/forms.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ def test_username_validity(self):
199199
self.assertEqual(form['username'].errors,
200200
[u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
201201

202+
def test_bug_14242(self):
203+
# A regression test, introduce by adding an optimization for the
204+
# UserChangeForm.
205+
206+
class MyUserForm(UserChangeForm):
207+
def __init__(self, *args, **kwargs):
208+
super(MyUserForm, self).__init__(*args, **kwargs)
209+
self.fields['groups'].help_text = 'These groups give users different permissions'
210+
211+
class Meta(UserChangeForm.Meta):
212+
fields = ('groups',)
213+
214+
# Just check we can create it
215+
form = MyUserForm({})
216+
217+
202218
class PasswordResetFormTest(TestCase):
203219

204220
fixtures = ['authtestdata.json']

0 commit comments

Comments
 (0)