简体   繁体   English

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

[英]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 : 当我尝试使用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 ''

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

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 编辑2:即使删除db后我不还原db,也存在问题

Please help! 请帮忙!

You have defined REQUIRED_FIELDS as a list containing a single empty string. 您已将REQUIRED_FIELDS定义为包含单个空字符串的列表。 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. Django试图确保提供所有字段,但是正如错误所言,空字符串不是该模型上的字段。

You could fix it by setting that value to an empty list instead: 您可以通过将该值设置为空列表来解决此问题:

REQUIRED_FIELDS = []

暂无
暂无

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

相关问题 django.core.exceptions.FieldDoesNotExist:用户没有名为“用户名”的字段 - django.core.exceptions.FieldDoesNotExist: User has no field named 'username' django.core.exceptions.FieldDoesNotExist:BuildingAddress没有名为“ False”的字段 - django.core.exceptions.FieldDoesNotExist: BuildingAddress has no field named 'False' django.core.exceptions.FieldDoesNotExist:模型在0x7fc5ae8836e0处没有名为<function SET_NULL的字段> - django.core.exceptions.FieldDoesNotExist: model has no field named <function SET_NULL at 0x7fc5ae8836e0> FieldDoesNotExist at /accounts/signup/,用户没有名为“用户名”的字段 - FieldDoesNotExist at /accounts/signup/, User has no field named 'username' Django生产服务器上的FieldDoesNotExist错误 - FieldDoesNotExist error on Django production server FieldDoesNotExist:ManyToManyField 没有名为 None 的字段 - FieldDoesNotExist: ManyToManyField has no field named None Django:在模型中更改字段类型时出现FieldDoesNotExist错误 - Django: Got FieldDoesNotExist error when changing field type in models 带有list_filter错误的Wagtail ModelAdmin:FieldDoesNotExist:区域没有名为“ p”的字段 - Wagtail ModelAdmin with list_filter error: FieldDoesNotExist: Region has no field named 'p' Django createsuperuser 错误未指定 Django 设置 - Django createsuperuser error No Django settings specified Django 1.8引发错误:django.core.exceptions.AppRegistryNotReady:应用尚未加载 - Django 1.8 throws an error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM