首页 > 解决方案 > 自定义用户模型 Django 错误,没有这样的表

问题描述

模型.py

class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        if not email:
            raise ValueError('Users must have an email address')
        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user


    def create_superuser(self, email, password):
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)  # a admin user; non super-user
    admin = models.BooleanField(default=False)  # a superuser
    phone = models.IntegerField()
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def get_full_name(self):            
        return self.email

    def get_short_name(self):
        return self.email

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin

    objects = UserManager()

设置.py

AUTH_USER_MODEL = 'Accounts.User'

错误:

return Database.Cursor.execute(self, query, params)                                                                 
django.db.utils.OperationalError: no such table: Accounts_user 

我已经创建了一个模型用户,但是当我尝试创建一个超级用户时,我收到了一个错误,该表不存在,我已经尝试了多次 makemigrations 和 migrate 命令,但似乎没有任何解决问题的方法

我什至无法在管理面板中打开表格,有人可以帮我解决这个问题吗

标签: pythondjangodjango-modelsdjango-rest-frameworkdjango-auth-models

解决方案


我尝试给出几个提示:

  1. 确保您的模型在Accounts您创建的应用程序中

  2. 检查Accounts应用程序是否已注册settings.py


推荐阅读