Commits

Luke Plant committed f1ac0a1

[1.2.X] Fixed #14242 - UserChangeForm subclasses without 'user_permissions' field causes KeyError

This was a regression introduced by [13684]

Thanks to adammckerlie@gmail.com for report.

Backport of [13702] from trunk.

Comments (0)

Files changed (2)

django/contrib/auth/forms.py

 
     def __init__(self, *args, **kwargs):
         super(UserChangeForm, self).__init__(*args, **kwargs)
-        self.fields['user_permissions'].queryset = self.fields['user_permissions'].queryset.select_related('content_type')
+        f = self.fields.get('user_permissions', None)
+        if f is not None:
+            f.queryset = f.queryset.select_related('content_type')
 
 class AuthenticationForm(forms.Form):
     """

django/contrib/auth/tests/forms.py

         self.assertEqual(form['username'].errors,
                          [u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
 
+    def test_bug_14242(self):
+        # A regression test, introduce by adding an optimization for the
+        # UserChangeForm.
+
+        class MyUserForm(UserChangeForm):
+            def __init__(self, *args, **kwargs):
+                super(MyUserForm, self).__init__(*args, **kwargs)
+                self.fields['groups'].help_text = 'These groups give users different permissions'
+
+            class Meta(UserChangeForm.Meta):
+                fields = ('groups',)
+
+        # Just check we can create it
+        form = MyUserForm({})
+
+
 class PasswordResetFormTest(TestCase):
 
     fixtures = ['authtestdata.json']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.