Migrating from django user model to a custom user model

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Migrating from django user model to a custom user model

Kakar Nyori

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 the 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? Could you guys please help me out.


Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4acb65c3-04a2-47b8-8a51-fbb0ec587f37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Migrating from django user model to a custom user model

Tim Graham-2
You can find an overview of how to migrate to a custom user model in https://code.djangoproject.com/ticket/25313.

On Tuesday, March 14, 2017 at 11:42:34 PM UTC-4, Kakar Nyori wrote:

I am following these two references (<a href="https://code.djangoproject.com/ticket/25313" rel="nofollow" style="color:rgb(0,89,153)" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcode.djangoproject.com%2Fticket%2F25313\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGjX6dugydUGWutOCLpzlmxR1lzkA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcode.djangoproject.com%2Fticket%2F25313\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGjX6dugydUGWutOCLpzlmxR1lzkA&#39;;return true;">one and <a href="http://django-authtools.readthedocs.io/en/latest/how-to/migrate-to-a-custom-user-model.html" rel="nofollow" style="color:rgb(0,89,153)" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdjango-authtools.readthedocs.io%2Fen%2Flatest%2Fhow-to%2Fmigrate-to-a-custom-user-model.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEF-qGbG2fIvS-v5mu-rL_JOV2nzQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdjango-authtools.readthedocs.io%2Fen%2Flatest%2Fhow-to%2Fmigrate-to-a-custom-user-model.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEF-qGbG2fIvS-v5mu-rL_JOV2nzQ&#39;;return true;">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 the 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? Could you guys please help me out.


Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a111086c-16f2-48b3-9c47-0f0a28bb34d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.