Fixed #27515 -- Made AuthenticationForm's username field use the max_length from the model field.

Thanks Ramin Farajpour Cami for the report.
This commit is contained in:
Lucas Connors 2017-08-17 14:08:56 -07:00 committed by Tim Graham
parent d233391208
commit 5ceaf14686
4 changed files with 18 additions and 5 deletions

View File

@ -478,6 +478,7 @@ answer newbie questions, and generally made Django that much better:
Loïc Bistuer <loic.bistuer@sixmedia.com> Loïc Bistuer <loic.bistuer@sixmedia.com>
Lowe Thiderman <lowe.thiderman@gmail.com> Lowe Thiderman <lowe.thiderman@gmail.com>
Luan Pablo <luanpab@gmail.com> Luan Pablo <luanpab@gmail.com>
Lucas Connors <http://www.revolutiontech.ca>
Luciano Ramalho Luciano Ramalho
Ludvig Ericson <ludvig.ericson@gmail.com> Ludvig Ericson <ludvig.ericson@gmail.com>
Luis C. Berrocal <luis.berrocal.1942@gmail.com> Luis C. Berrocal <luis.berrocal.1942@gmail.com>

View File

@ -155,10 +155,7 @@ class AuthenticationForm(forms.Form):
Base class for authenticating users. Extend this to get a form that accepts Base class for authenticating users. Extend this to get a form that accepts
username/password logins. username/password logins.
""" """
username = UsernameField( username = UsernameField(widget=forms.TextInput(attrs={'autofocus': True}))
max_length=254,
widget=forms.TextInput(attrs={'autofocus': True}),
)
password = forms.CharField( password = forms.CharField(
label=_("Password"), label=_("Password"),
strip=False, strip=False,
@ -182,8 +179,9 @@ class AuthenticationForm(forms.Form):
self.user_cache = None self.user_cache = None
super().__init__(*args, **kwargs) 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.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: if self.fields['username'].label is None:
self.fields['username'].label = capfirst(self.username_field.verbose_name) self.fields['username'].label = capfirst(self.username_field.verbose_name)

View File

@ -19,5 +19,6 @@ class CustomEmailField(AbstractBaseUser):
is_active = models.BooleanField(default=True) is_active = models.BooleanField(default=True)
EMAIL_FIELD = 'email_address' EMAIL_FIELD = 'email_address'
USERNAME_FIELD = 'username'
objects = CustomEmailFieldUserManager() objects = CustomEmailFieldUserManager()

View File

@ -377,6 +377,19 @@ class AuthenticationFormTest(TestDataMixin, TestCase):
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertEqual(form.non_field_errors(), []) 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') @override_settings(AUTH_USER_MODEL='auth_tests.IntegerUsernameUser')
def test_username_field_max_length_defaults_to_254(self): def test_username_field_max_length_defaults_to_254(self):
self.assertIsNone(IntegerUsernameUser._meta.get_field('username').max_length) self.assertIsNone(IntegerUsernameUser._meta.get_field('username').max_length)