Django中的自定义用户模型:`no such table:auth_user` [英] Custom user models in Django: `no such table: auth_user`

查看:4683
本文介绍了Django中的自定义用户模型:`no such table:auth_user`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我以前的问题,我编辑了django注册模块,具有以下修改:



myapp / models.py

  from django.contrib.auth.models import UserManager 
from django.contrib.auth.models import AbstractUser

class AccountManager(UserManager):
def create_user(self,email,password = None,** kwargs):
如果不是电子邮件:
raise ValueError('Users must have一个有效的电子邮件地址')
如果不是kwargs.get('username'):
raise ValueError('用户必须有一个有效的用户名')

account = self $ mod $(
email = self.normalize_email(email),
username = kwargs.get('username'),
#year_of_birth = kwargs.get('year_of_birth'),
#M ODEL = kwargs.get('MODEL_NAME'),

account.set_password(密码)
account.save()

返回帐户

def create_superuser(self,email,password,** kwargs):
account = self.create_user(email,password,** kwargs)
account.is_staff = True
account。 is_superuser = True
account.save()

返回帐户

类帐户(AbstractUser):
#email = models.EmailField(unique = True )
points = models.FloatField(default = 100.0)
#ADD你的模型在这里

objects = AccountManager()

def __str __(self) :
返回帐户:+ self.username

myproject / settings.py

  AUTH_USER_MODEL ='myapp.Account'

现在其余的应用程序工作(即指的是帐户,它还具有来自 User 模型的基本字段)。但是, django-registration 模块本身现在不起作用。当我发送新的注册表单(用户名,电子邮件和密码)时,我收到错误:

  OperationalError at / accounts /注册/ 
没有这样的表:auth_user

我尝试了很多组合的 makemigrations 迁移(也与 - 假的/ / code>和 - fake-initial 参数)但是(我猜) c $ a> 表不使用新设置进行初始化。 b
$ b

如何解决这个问题,所以我可以继续注册新的帐户模型,而不会损害默认的 auth 模块的用户模型?



感谢任何帮助,



编辑



当我试图在shell中看到用户时,我收到了这个错误,这使得事情有点清楚:

 >>>来自django.contrib.auth.models import User 
>>> users = User.objects.all()
AttributeError:Manager不可用;用户已被替换为'myapp.Account'

所以看起来像我的新模型帐户已覆盖默认的用户模型。如果是这样,为什么注册模块寻找 auth_user 表?

解决方案

p>如果应用了该应用的迁移,则无法交换任何引用该应用的应用的用户模型。尝试通过创建新的数据库,重新应用迁移,然后从旧数据库移动数据


According to the answer to my previous question, I edited the django-registration module with the following modifications:

in myapp/models.py:

from django.contrib.auth.models import UserManager
from django.contrib.auth.models import AbstractUser

class AccountManager(UserManager):
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')
        if not kwargs.get('username'):
            raise ValueError('Users must have a valid username.')

        account = self.model(
            email=self.normalize_email(email), 
            username=kwargs.get('username'), 
            #year_of_birth = kwargs.get('year_of_birth'),
            #MODEL = kwargs.get('MODEL_NAME'),
        )
        account.set_password(password)
        account.save()

        return account

    def create_superuser(self, email, password, **kwargs):
        account = self.create_user(email, password, **kwargs)
        account.is_staff = True
        account.is_superuser = True
        account.save()

        return account

class Account(AbstractUser):
    #email = models.EmailField(unique=True)
    points = models.FloatField(default = 100.0)
    #ADD YOUR MODELS HERE

    objects = AccountManager()

    def __str__(self):
        return "Account: "+self.username

and in myproject/settings.py:

AUTH_USER_MODEL = 'myapp.Account'

now the rest of the application works (i.e. refers to Account which also has base fields from the User model). However, the django-registration module itself is not working now. When I send a new registration form (with username, email and password), I get the error:

OperationalError at /accounts/register/
no such table: auth_user

I tried a lot of combinations of makemigrations and migrate (also with --fake and --fake-initial arguments) but (I guess) the auth table is not initializing with the new settings.

How can I fix this so I can keep registering with the new Account model while not harming the default auth module's User model?

Thanks for any help,

Edit

When I tried to see the users in shell, I got this error which makes things a little bit clearer:

>>> from django.contrib.auth.models import User
>>> users = User.objects.all()
AttributeError: Manager isn't available; User has been swapped for 'myapp.Account'

So it looks like my new model Account has overwritten the default User model. If so, why is the registration module looking for the auth_user table? Doesn't the modification in settings.py aim to fix this?

解决方案

You can't swap user model for any app that refers to it, if migrations for that app have been applied. Try to re-create your database by creating new one, reapplying migrations on it and then moving data from old database

这篇关于Django中的自定义用户模型:`no such table:auth_user`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆