【问题标题】:django.db.utils.OperationalError: no such table: MainApp_user when i try to create superuserdjango.db.utils.OperationalError: no such table: MainApp_user 当我尝试创建超级用户时
【发布时间】:2021-11-02 15:17:34
【问题描述】:

我制作了一些 Web 应用程序,几乎在开发过程的最后,我决定自定义用户模型以制作包含许多其他信息的个人资料页面。最近我发现了一个视频,其中解释了 django docs 中的示例,我制作了与之前相同的代码,但起初我删除了我的文件 db.sqlite3,现在当我尝试创建超级用户时,我总是会捕捉到下一个错误: django.db.utils.OperationalError: no such table: MainApp_user

这是我的 models.py

class MyUserManager(BaseUserManager):
    def create_user(self, username, password):
        if not username:
            raise ValueError("Mailname field is empty")
        if not password:
            raise ValueError("You have to set password")

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


class User(AbstractBaseUser):
    username            = models.CharField(max_length=30, unique=True)
    password            = models.CharField(max_length=64)

    name                = models.CharField(max_length=30, blank=True)
    surname             = models.CharField(max_length=30, blank=True)  
    
    avatar              = models.ImageField(width_field=512, height_field=512)

    email_link          = models.CharField(max_length=64, blank=True, null=True)
    bio                 = models.CharField(max_length=512, blank=True, null=True)
    
    registered          = models.DateField(auto_now_add=True)

    USERNAME_FIELD      = "username"
    REQUIRED_FIELDS     = ['password']
    objects             = MyUserManager()

    def __str__(self):
        return self.username

我还在 settings.py 中添加了下一个变量:

AUTH_USER_MODEL = "MainApp.User"

为什么会发生此错误以及如何解决,请帮助。

***Ofc,我确实迁移到了数据库

【问题讨论】:

    标签: python django django-custom-user operationalerror


    【解决方案1】:

    我确实找到了解决方案。就我而言,当我删除 db.sqlite3 文件时,我还删除了应用程序中的“迁移”文件夹。当然,我已经重新创建了它,但 问题 只是因为我忘记在 迁移中添加 init.py文件夹。 django 在应用迁移后只在数据库中创建了系统表(auth_users、contenttypes 等),它没有保存我自己的模型。因此,如果您遇到任何类似的问题,请记住 init.pymigrations 文件夹中。 Good luck !

    【讨论】:

    • 你的意思是__ init__.py
    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2021-08-03
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    相关资源
    最近更新 更多