繁体   English   中英

Django createsuperuser抛出django.core.exceptions.FieldDoesNotExist:用户没有名为''的字段错误

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

当我尝试使用python manage.py createsuperuser命令创建超级用户时,抛出以下错误:

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 ''

我正在扩展Django用户模型,它工作正常,但是当我删除数据库然后再次成功恢复数据库时,但是当我要创建超级用户时,它将引发此错误。

编辑:这是用户模型的代码:

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

编辑2:即使删除db后我不还原db,也存在问题

请帮忙!

您已将REQUIRED_FIELDS定义为包含单个空字符串的列表。 Django试图确保提供所有字段,但是正如错误所言,空字符串不是该模型上的字段。

您可以通过将该值设置为空列表来解决此问题:

REQUIRED_FIELDS = []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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