简体   繁体   中英

Django createsuperuser throws django.core.exceptions.FieldDoesNotExist: User has no field named ' ' error

When i try to create a superuser using python manage.py createsuperuser command, it throws the following error :

Username: wcud
Traceback (most recent call last):
  File "/home/Music/ENV/lib/python3.5/site-packages/django/db/models/options.py", line 617, in get_field
    return self.fields_map[field_name]
KeyError: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/Music/ENV/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 129, in handle
    field = self.UserModel._meta.get_field(field_name)
  File "/home/Music/ENV/lib/python3.5/site-packages/django/db/models/options.py", line 619, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: User has no field named ''

I am extending the Django user model it was working fine but when i deleted my database and then i again restore the database successfully, but when i am going to create the superuser it throws this error.

EDIT: Here is the code for user model :

class User(AbstractBaseUser):
    username = models.CharField(max_length=64, unique=True, validators=[RegexValidator(regex=USERNAME_REGX, message="Username only contain A-Z,a-z,0-9 or . _ + - $", code="Invalid Username")])
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        blank=True,
        null=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['']

    def __str__(self):
        return self.username

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username


class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    phone_regex = RegexValidator(regex=r'^\d{10}$', message="Please Enter Correct Mobile Number...")
    phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=False, unique=True)
    state = models.CharField(max_length=50, choices=STATES, blank=True, default=STATES[0][0])
    #date_of_birth = models.DateTimeField(auto_now=False, blank=True)
    balance = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    bonus = models.DecimalField(max_digits=4, decimal_places=2, default=25)
    widhdrawable_balance = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    match_played = models.IntegerField(default=0)
    total_wins = models.IntegerField(default=0)
    is_email_confirmed = models.BooleanField(default=False)

    def __str__(self):
        return self.user.username

EDIT 2 : Problem exists even if I don't restore the db after deletiong of db

Please help!

You have defined REQUIRED_FIELDS as a list containing a single empty string. Django is attempting to ensure that all the fields are supplied, but as the error says, the empty string is not a field on that model.

You could fix it by setting that value to an empty list instead:

REQUIRED_FIELDS = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM