[4.0.x] Fixed #33178 -- Made createsuperuser validate required fields passed in options in interactive mode.

Backport of b1b26b37af from main.
This commit is contained in:
Christophe Henry 2021-10-08 15:33:09 +02:00 committed by Mariusz Felisiak
parent b55df4c74a
commit 048fbf9c89
2 changed files with 25 additions and 0 deletions

View File

@ -124,6 +124,8 @@ class Command(BaseCommand):
for field_name in self.UserModel.REQUIRED_FIELDS: for field_name in self.UserModel.REQUIRED_FIELDS:
field = self.UserModel._meta.get_field(field_name) field = self.UserModel._meta.get_field(field_name)
user_data[field_name] = options[field_name] user_data[field_name] = options[field_name]
if user_data[field_name] is not None:
user_data[field_name] = field.clean(user_data[field_name], None)
while user_data[field_name] is None: while user_data[field_name] is None:
message = self._get_input_message(field) message = self._get_input_message(field)
input_value = self.get_input_data(field, message) input_value = self.get_input_data(field, message)

View File

@ -531,6 +531,29 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
test(self) test(self)
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithFK')
def test_validate_fk_via_option_interactive(self):
email = Email.objects.create(email='mymail@gmail.com')
Group.objects.all().delete()
nonexistent_group_id = 1
msg = f'group instance with id {nonexistent_group_id} does not exist.'
@mock_inputs({
'password': 'nopasswd',
'Username (Email.id): ': email.pk,
'Email (Email.email): ': email.email,
})
def test(self):
with self.assertRaisesMessage(CommandError, msg):
call_command(
'createsuperuser',
group=nonexistent_group_id,
stdin=MockTTY(),
verbosity=0,
)
test(self)
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2m') @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2m')
def test_fields_with_m2m(self): def test_fields_with_m2m(self):
new_io = StringIO() new_io = StringIO()