从Django用户模型迁移到自定义用户模型 [英] Migrating from django user model to a custom user model

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

问题描述

我正在关注这两个参考文献(一个两个),以便按顺序拥有自定义用户模型

I am following these two references (one and two) to have a custom user model in order to authenticate via email and also to add an extra field to it.

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        unique=True,
        max_length=254,
    )
    mobile_number = models.IntegerField(unique=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()
    ...
    ...    
    class Meta:
        db_table = 'auth_user'
    ...
    ...

如您所见,我已经将 db_table ='auth_user'添加到该类的Meta字段中。另外,我已经将 AUTH_USER_MODEL ='accounts.User'和用户模型应用程序(即帐户)包含在 INSTALLED_APPS 中在settings.py中。另外,我从应用程序中删除了迁移文件夹。

As you can see, I have added the db_table='auth_user' into the Meta fields of the class. Also, I have included AUTH_USER_MODEL = 'accounts.User' and User model app (i.e., accounts)into the INSTALLED_APPS in settings.py. Further more, I deleted the migrations folder from the app.

然后尝试迁移:

$ python manage.py makemigrations accounts
Migrations for 'accounts':
  accounts/migrations/0001_initial.py:
    - Create model User

$ python manage.py migrate accounts

这给了我一个错误:


django.db.migrations.exceptions.InconsistentMigrationHistory:迁移admin.0001_initial在其依赖项帐户之前应用。0001_initial在数据库'default'上。

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.

如何从现有的django用户模型迁移到自定义用户模型?

How can I migrate from the existing django user model into a custom user model?

推荐答案

您必须从迁移历史记录中清除admin,auth,contenttypes和会话,并删除表。首先,删除应用程序的迁移文件夹,然后键入以下内容:

You have to clear admin, auth, contenttypes, and sessions from the migration history and also drop the tables. First, remove the migration folders of your apps and then type the following:

python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero

之后,您可以运行 makemigrations帐户 migrate帐户

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

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