#25313: Document how to migrate from a built-in User model to a custom User 
model
-------------------------------+------------------------------------
     Reporter:  Carl Meyer     |                    Owner:  nobody
         Type:  New feature    |                   Status:  new
    Component:  Documentation  |                  Version:  1.8
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------

Comment (by BrandonWoodward):

 I believe the following to be the easiest method.

 1. Follow the preparations in
 [https://code.djangoproject.com/ticket/25313#comment:18 Carsten's
 solution].

 2. Create a new app with no existing migrations and migrate an empty
 migration.

 {{{#!python
 ./manage.py startapp user
 ./manage.py makemigrations user --empty
 ./manage.py migrate user
 }}}

 3. Update the migration to match the timestamp of the initial auth
 migration.

 {{{#!python
 ./manage.py shell
 >>> from django.db.migrations.recorder import MigrationRecorder
 >>> auth_initial = MigrationRecorder.Migration.objects.get(app='auth',
 name='0001_initial')
 >>> user_initial = MigrationRecorder.Migration.objects.get(app='user',
 name='0001_initial')
 >>> user_initial.applied = auth_initial.applied
 >>> user_initial.save()
 }}}

 4. Delete `user 0001_initial.py`, set `AUTH_USER_MODEL = 'user.user'` in
 your settings then run `./manage.py makemigrations` after creating the
 following `user` model.

 {{{#!python
 from django.contrib.auth import models as auth_models

 class User(auth_models.AbstractUser):
     class Meta:
         db_table = 'auth_user'
 }}}

 5. Update the content types

 {{{#!python
 ./manage.py shell
 >>> from django.contrib.contenttypes.models import ContentType
 >>> ContentType.objects.filter(app_label='auth',
 model='user').update(app_label='user')
 }}}

 6. Delete the model inherited from `auth_models.AbstractUser` and create
 your custom user model. Run `makemigrations` then `migrate` after making
 your changes and from this point on everything should be working properly.

 {{{#!python
 class User(auth_models.AbstractBaseUser,
            auth_models.PermissionsMixin):
     ...
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25313#comment:21>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.2ad2ee9bfff74a09f3091e918f5b0a45%40djangoproject.com.

Reply via email to