0

模型.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 命令,但似乎没有任何解决问题的方法

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

4

2 回答 2

0

我尝试给出几个提示:

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

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

于 2021-11-04T20:58:13.627 回答
0

一个小问题,使用默认命令

manage.py makemigration
manage.py migrate

没有更新 Accounts App User 表,所以必须使用

manage.py makemigration Accounts
manage.py migrate Accounts

这解决了错误

于 2021-11-05T19:11:32.413 回答