Ticket #33151: 33151.diff

File 33151.diff, 1.8 KB (added by Christophe Henry, 3 years ago)

Proposed patch to fix the bug

  • django/contrib/auth/management/commands/createsuperuser.py

    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  
    1111from django.core import exceptions
    1212from django.core.management.base import BaseCommand, CommandError
    1313from django.db import DEFAULT_DB_ALIAS
     14from django.db.models import Model
    1415from django.utils.text import capfirst
    1516
    1617
    class Command(BaseCommand):  
    184185                    if not value:
    185186                        raise CommandError('You must use --%s with --noinput.' % field_name)
    186187                    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)
    188201
    189202            self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
    190203            if options['verbosity'] >= 1:
Back to Top