diff --git a/AUTHORS b/AUTHORS index 80f277996b..cedff32fe0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -550,6 +550,7 @@ answer newbie questions, and generally made Django that much better: Thomas Steinacher Emil Stenström Johan C. Stöver + Chris Streeter Nowell Strite Thomas Stromberg Hannes Struß diff --git a/django/contrib/auth/tests/test_custom_user.py b/django/contrib/auth/tests/test_custom_user.py index a3a159880a..0f6ebbda55 100644 --- a/django/contrib/auth/tests/test_custom_user.py +++ b/django/contrib/auth/tests/test_custom_user.py @@ -156,6 +156,18 @@ class CustomUserNonUniqueUsername(AbstractBaseUser): app_label = 'auth' +class CustomUserNonListRequiredFields(AbstractBaseUser): + "A user with a non-list REQUIRED_FIELDS" + username = models.CharField(max_length=30, unique=True) + date_of_birth = models.DateField() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = 'date_of_birth' + + class Meta: + app_label = 'auth' + + class CustomUserBadRequiredFields(AbstractBaseUser): "A user with a non-unique username" username = models.CharField(max_length=30, unique=True) diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index fee0a29e7b..6c3718465d 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -174,6 +174,13 @@ class CreatesuperuserManagementCommandTestCase(TestCase): class CustomUserModelValidationTestCase(TestCase): + @override_settings(AUTH_USER_MODEL='auth.CustomUserNonListRequiredFields') + def test_required_fields_is_list(self): + "REQUIRED_FIELDS should be a list." + new_io = StringIO() + get_validation_errors(new_io, get_app('auth')) + self.assertIn("The REQUIRED_FIELDS must be a list or tuple.", new_io.getvalue()) + @override_settings(AUTH_USER_MODEL='auth.CustomUserBadRequiredFields') def test_username_not_in_required_fields(self): "USERNAME_FIELD should not appear in REQUIRED_FIELDS." diff --git a/django/core/management/validation.py b/django/core/management/validation.py index a6d6a76985..a64c6e815c 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -51,6 +51,10 @@ def get_validation_errors(outfile, app=None): # If this is the current User model, check known validation problems with User models if settings.AUTH_USER_MODEL == '%s.%s' % (opts.app_label, opts.object_name): + # Check that REQUIRED_FIELDS is a list + if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)): + e.add(opts, 'The REQUIRED_FIELDS must be a list or tuple.') + # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS. if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS: e.add(opts, 'The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.')