diff options
| author | Lucas Connors <lucas.revolutiontech@gmail.com> | 2017-08-17 14:08:56 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-10-20 11:13:26 -0400 |
| commit | 5ceaf14686ce626404afb6a5fbd3d8286410bf13 (patch) | |
| tree | 194cbda5c0c2c4ce866727aeed94544eb556a051 | |
| parent | d2333912085fc3bd827295f2bc8253d6723c242b (diff) | |
Fixed #27515 -- Made AuthenticationForm's username field use the max_length from the model field.
Thanks Ramin Farajpour Cami for the report.
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/contrib/auth/forms.py | 8 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_custom_email_field.py | 1 | ||||
| -rw-r--r-- | tests/auth_tests/test_forms.py | 13 |
4 files changed, 18 insertions, 5 deletions
@@ -478,6 +478,7 @@ answer newbie questions, and generally made Django that much better: Loïc Bistuer <loic.bistuer@sixmedia.com> Lowe Thiderman <lowe.thiderman@gmail.com> Luan Pablo <luanpab@gmail.com> + Lucas Connors <http://www.revolutiontech.ca> Luciano Ramalho Ludvig Ericson <ludvig.ericson@gmail.com> Luis C. Berrocal <luis.berrocal.1942@gmail.com> diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index a5de5bf650..3b14a1791e 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -155,10 +155,7 @@ class AuthenticationForm(forms.Form): Base class for authenticating users. Extend this to get a form that accepts username/password logins. """ - username = UsernameField( - max_length=254, - widget=forms.TextInput(attrs={'autofocus': True}), - ) + username = UsernameField(widget=forms.TextInput(attrs={'autofocus': True})) password = forms.CharField( label=_("Password"), strip=False, @@ -182,8 +179,9 @@ class AuthenticationForm(forms.Form): self.user_cache = None super().__init__(*args, **kwargs) - # Set the label for the "username" field. + # Set the max length and label for the "username" field. self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD) + self.fields['username'].max_length = self.username_field.max_length or 254 if self.fields['username'].label is None: self.fields['username'].label = capfirst(self.username_field.verbose_name) diff --git a/tests/auth_tests/models/with_custom_email_field.py b/tests/auth_tests/models/with_custom_email_field.py index a98b02b8f1..27b1f810f2 100644 --- a/tests/auth_tests/models/with_custom_email_field.py +++ b/tests/auth_tests/models/with_custom_email_field.py @@ -19,5 +19,6 @@ class CustomEmailField(AbstractBaseUser): is_active = models.BooleanField(default=True) EMAIL_FIELD = 'email_address' + USERNAME_FIELD = 'username' objects = CustomEmailFieldUserManager() diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py index f7d0e71ea9..f15aef37e3 100644 --- a/tests/auth_tests/test_forms.py +++ b/tests/auth_tests/test_forms.py @@ -377,6 +377,19 @@ class AuthenticationFormTest(TestDataMixin, TestCase): self.assertTrue(form.is_valid()) self.assertEqual(form.non_field_errors(), []) + @override_settings(AUTH_USER_MODEL='auth_tests.CustomEmailField') + def test_username_field_max_length_matches_user_model(self): + self.assertEqual(CustomEmailField._meta.get_field('username').max_length, 255) + data = { + 'username': 'u' * 255, + 'password': 'pwd', + 'email': 'test@example.com', + } + CustomEmailField.objects.create_user(**data) + form = AuthenticationForm(None, data) + self.assertEqual(form.fields['username'].max_length, 255) + self.assertEqual(form.errors, {}) + @override_settings(AUTH_USER_MODEL='auth_tests.IntegerUsernameUser') def test_username_field_max_length_defaults_to_254(self): self.assertIsNone(IntegerUsernameUser._meta.get_field('username').max_length) |
