Migrating from django user model to a custom user model

陌路散爱 提交于 2019-12-20 18:35:09

问题


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'
    ...
    ...

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.

Then tried migrating:

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

$ python manage.py migrate accounts

Which gives me an error:

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

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


回答1:


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

Afterwards, you can run makemigrations accounts and migrate accounts.




回答2:


As in my particular case, the other answers did not help (the error still occured even after I tried to drop the tables with migrate ... zero and even after I deleted the migrations folder), the following helped, but I was at the very beginning and therefore it was no problem to just delete the db.sqlite3 file which is created whenever you migrate the first time. (Depending on your settings.py you might have a different database-file).

You really can only do this if you are sure that you don't lose important data from your database file (e.g. you do not yet have much information stored in the database and it is not difficult to start over again), and you will need to migrate everything again.




回答3:


Delete the existing all the tables from data base.[Note : data will be lost]

Delete pycache and migrations from all the apps.

Run migrations for your relative app

python manage.py makemigrations users

Migrate the tables to database

python manage.py migrate



回答4:


You need to run:

python manage.py makemigrations accounts

Before executing the initial manage.py migrate (by initial I mean at the very first time you run migrate on your project)

it is recommended to set up your custom User model at the start of your project so you'll have the "accounts" app migrated at the same time as the admin,auth,contenttypes,sessions tables are created.

but if you have created your tables already, then you should follow the instructions as @krishna-chandak described: https://stackoverflow.com/a/53599345/5950111

you can read the docs : https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project




回答5:


I know it's rather an old question, but for people googling this topic like me today, here is a solution without deleting migrations, dropping the tables, and other nasty stuff)

https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/



来源:https://stackoverflow.com/questions/42794212/migrating-from-django-user-model-to-a-custom-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!