diff options
| author | Yan Mitrofanov <mmxlviii@mail.ru> | 2020-08-13 13:27:00 +0300 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-08-14 11:08:20 +0200 |
| commit | b88f98738fbbdf119f4ff001e4eccf72a77d9518 (patch) | |
| tree | 2eba1db3a30b63b422a5c6a24d0f571267a57592 /django | |
| parent | 552bb82928a8dbc0531fdffad2131ac34fbbc1df (diff) | |
Fixed #31878 -- Made createsuperuser respect --database option in default usernames.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/auth/management/__init__.py | 9 | ||||
| -rw-r--r-- | django/contrib/auth/management/commands/createsuperuser.py | 2 |
2 files changed, 7 insertions, 4 deletions
diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index deda238c78..1170cb0b20 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -101,14 +101,15 @@ def get_system_username(): return result -def get_default_username(check_db=True): +def get_default_username(check_db=True, database=DEFAULT_DB_ALIAS): """ Try to determine the current system user's username to use as a default. :param check_db: If ``True``, requires that the username does not match an existing ``auth.User`` (otherwise returns an empty string). + :param database: The database where the unique check will be performed. :returns: The username, or an empty string if no username can be - determined. + determined or the suggested username is already taken. """ # This file is used in apps.py, it should not trigger models import. from django.contrib.auth import models as auth_app @@ -137,7 +138,9 @@ def get_default_username(check_db=True): # Don't return the default username if it is already taken. if check_db and default_username: try: - auth_app.User._default_manager.get(username=default_username) + auth_app.User._default_manager.db_manager(database).get( + username=default_username, + ) except auth_app.User.DoesNotExist: pass else: diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index 3b76477f01..3eb8abff06 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -97,7 +97,7 @@ class Command(BaseCommand): fake_user_data = {} if hasattr(self.stdin, 'isatty') and not self.stdin.isatty(): raise NotRunningInTTYException - default_username = get_default_username() + default_username = get_default_username(database=database) if username: error_msg = self._validate_username(username, verbose_field_name, database) if error_msg: |
