diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py
index 3b76477f01..d1cba53a0f 100644
a
|
b
|
from django.contrib.auth.password_validation import validate_password
|
11 | 11 | from django.core import exceptions |
12 | 12 | from django.core.management.base import BaseCommand, CommandError |
13 | 13 | from django.db import DEFAULT_DB_ALIAS |
| 14 | from django.db.models import Model |
14 | 15 | from django.utils.text import capfirst |
15 | 16 | |
16 | 17 | |
… |
… |
class Command(BaseCommand):
|
184 | 185 | if not value: |
185 | 186 | raise CommandError('You must use --%s with --noinput.' % field_name) |
186 | 187 | field = self.UserModel._meta.get_field(field_name) |
187 | | user_data[field_name] = field.clean(value, None) |
| 188 | if field.many_to_one: |
| 189 | field_model = field.remote_field.model._meta.concrete_model |
| 190 | if isinstance(value, field_model): |
| 191 | user_data[field_name] = field.clean(value, None) |
| 192 | else: |
| 193 | try: |
| 194 | user_data[field_name] = field_model.objects.get(pk=field.clean(value, None)) |
| 195 | except field_model.DoesNotExist: |
| 196 | raise CommandError( |
| 197 | 'No record of %s was found with primary key %s' % (field_model.name, value) |
| 198 | ) |
| 199 | else: |
| 200 | user_data[field_name] = field.clean(value, None) |
188 | 201 | |
189 | 202 | self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) |
190 | 203 | if options['verbosity'] >= 1: |