diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py
index de0b467..b352ae8 100644
a
|
b
|
class Command(BaseCommand):
|
88 | 88 | verbose_field_name = self.username_field.verbose_name |
89 | 89 | while username is None: |
90 | 90 | if not username: |
91 | | input_msg = capfirst(verbose_field_name) |
| 91 | input_msg = capfirst(verbose_field_name) + ' (%s.%s)' % (self.username_field.rel.to._meta.object_name, self.username_field.rel.field_name) if self.username_field.rel else '' |
92 | 92 | if default_username: |
93 | 93 | input_msg = "%s (leave blank to use '%s')" % ( |
94 | 94 | input_msg, default_username) |
diff --git a/django/contrib/auth/tests/custom_user.py b/django/contrib/auth/tests/custom_user.py
index cfa998a..77e8477 100644
a
|
b
|
class CustomUserManager(BaseUserManager):
|
40 | 40 | |
41 | 41 | class CustomUserWithFKManager(BaseUserManager): |
42 | 42 | def create_superuser(self, username, email, group, password): |
43 | | user = self.model(username=username, email_id=email, group_id=group) |
| 43 | user = self.model(username_id=username, email_id=email, group_id=group) |
44 | 44 | user.set_password(password) |
45 | 45 | user.save(using=self._db) |
46 | 46 | return user |
… |
… |
class CustomUser(AbstractBaseUser):
|
96 | 96 | |
97 | 97 | |
98 | 98 | class CustomUserWithFK(AbstractBaseUser): |
99 | | username = models.CharField(max_length=30, unique=True) |
100 | | email = models.ForeignKey(Email, to_field='email') |
| 99 | username = models.ForeignKey(Email, related_name='primary') |
| 100 | email = models.ForeignKey(Email, to_field='email', related_name='secondary') |
101 | 101 | group = models.ForeignKey(Group) |
102 | 102 | |
103 | 103 | custom_objects = CustomUserWithFKManager() |
diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py
index e774d3a..1fe8cee 100644
a
|
b
|
class CreatesuperuserManagementCommandTestCase(TestCase):
|
350 | 350 | self.assertIs(command.stdin, sys.stdin) |
351 | 351 | |
352 | 352 | @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK') |
353 | | def test_required_field_with_fk(self): |
| 353 | def test_fields_with_fk(self): |
354 | 354 | new_io = six.StringIO() |
355 | 355 | group = Group.objects.create(name='mygroup') |
356 | 356 | email = Email.objects.create(email='mymail@gmail.com') |
357 | 357 | call_command( |
358 | 358 | 'createsuperuser', |
359 | 359 | interactive=False, |
360 | | username='user', |
361 | | email='mymail@gmail.com', |
| 360 | username=email.pk, |
| 361 | email=email.email, |
362 | 362 | group=group.pk, |
363 | 363 | stdout=new_io, |
364 | 364 | skip_checks=True, |
… |
… |
class CreatesuperuserManagementCommandTestCase(TestCase):
|
366 | 366 | command_output = new_io.getvalue().strip() |
367 | 367 | self.assertEqual(command_output, 'Superuser created successfully.') |
368 | 368 | u = CustomUserWithFK._default_manager.get(email=email) |
369 | | self.assertEqual(u.username, "user") |
| 369 | self.assertEqual(u.username, email) |
370 | 370 | self.assertEqual(u.group, group) |
371 | 371 | |
372 | 372 | non_existent_email = 'mymail2@gmail.com' |
… |
… |
class CreatesuperuserManagementCommandTestCase(TestCase):
|
375 | 375 | call_command( |
376 | 376 | 'createsuperuser', |
377 | 377 | interactive=False, |
378 | | username='user', |
| 378 | username=email.pk, |
379 | 379 | email=non_existent_email, |
380 | 380 | stdout=new_io, |
381 | 381 | skip_checks=True, |
382 | 382 | ) |
383 | 383 | |
384 | 384 | @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK') |
385 | | def test_required_fields_with_fk_interactive(self): |
| 385 | def test_fields_with_fk_interactive(self): |
386 | 386 | new_io = six.StringIO() |
387 | 387 | group = Group.objects.create(name='mygroup') |
388 | 388 | email = Email.objects.create(email='mymail@gmail.com') |
389 | 389 | |
390 | | @mock_inputs({'password': "nopasswd", 'username': "user", 'email': "mymail@gmail.com", 'group': group.pk}) |
| 390 | @mock_inputs({'password': "nopasswd", 'username': email.pk, 'email': email.email, 'group': group.pk}) |
391 | 391 | def test(self): |
392 | 392 | call_command( |
393 | 393 | 'createsuperuser', |
… |
… |
class CreatesuperuserManagementCommandTestCase(TestCase):
|
400 | 400 | command_output = new_io.getvalue().strip() |
401 | 401 | self.assertEqual(command_output, 'Superuser created successfully.') |
402 | 402 | u = CustomUserWithFK._default_manager.get(email=email) |
403 | | self.assertEqual(u.username, 'user') |
| 403 | self.assertEqual(u.username, email) |
404 | 404 | self.assertEqual(u.group, group) |
405 | 405 | |
406 | 406 | test(self) |
… |
… |
class CustomUserModelValidationTestCase(TestCase):
|
460 | 460 | ] |
461 | 461 | self.assertEqual(errors, expected) |
462 | 462 | |
| 463 | @override_settings(AUTH_USER_MODEL='auth.CustomUserWithFK') |
| 464 | @override_system_checks([check_user_model]) |
| 465 | def test_username_with_fk_non_unique(self): |
| 466 | "A non-unique USERNAME_FIELD should raise a model validation error." |
| 467 | |
| 468 | errors = checks.run_checks() |
| 469 | expected = [ |
| 470 | checks.Error( |
| 471 | ("'CustomUserWithFK.username' must be " |
| 472 | "unique because it is named as the 'USERNAME_FIELD'."), |
| 473 | hint=None, |
| 474 | obj=CustomUserWithFK, |
| 475 | id='auth.E003', |
| 476 | ), |
| 477 | ] |
| 478 | self.assertEqual(errors, expected) |
| 479 | |
463 | 480 | @override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername', |
464 | 481 | AUTHENTICATION_BACKENDS=[ |
465 | 482 | 'my.custom.backend', |