社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

django createsuperuser引发django.core.exceptions.fielddoesnotexist:用户没有名为“”的字段错误

FightWithCode • 6 年前 • 1746 次点击  

当我尝试使用python manage.py createsupuser命令创建超级用户时,它会引发以下错误:

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:即使删除数据库后不还原数据库,也存在问题

请帮助!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/34024
 
1746 次点击  
文章 [ 1 ]  |  最新文章 6 年前
Daniel Roseman
Reply   •   1 楼
Daniel Roseman    6 年前

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

您可以通过将该值设置为空列表来修复它:

REQUIRED_FIELDS = []