1 | | I believe the following to be the easiest method. |
2 | | |
3 | | 1. Follow the preparations in [https://code.djangoproject.com/ticket/25313#comment:18 Carsten's solution]. |
4 | | |
5 | | 2. Create a new app with no existing migrations and migrate an empty migration. |
6 | | |
7 | | {{{#!python |
8 | | ./manage.py startapp user |
9 | | ./manage.py makemigrations user --empty |
10 | | ./manage.py migrate user |
11 | | }}} |
12 | | |
13 | | 3. Update the migration to match the timestamp of the initial auth migration. |
14 | | |
15 | | {{{#!python |
16 | | ./manage.py shell |
17 | | >>> from django.db.migrations.recorder import MigrationRecorder |
18 | | >>> auth_initial = MigrationRecorder.Migration.objects.get(app='auth', name='0001_initial') |
19 | | >>> user_initial = MigrationRecorder.Migration.objects.get(app='user', name='0001_initial') |
20 | | >>> user_initial.applied = auth_initial.applied |
21 | | >>> user_initial.save() |
22 | | }}} |
23 | | |
24 | | 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. Essentially this step is making your first migration in the `user` app match `auth 0001_initial.py`. |
25 | | |
26 | | {{{#!python |
27 | | from django.contrib.auth import models as auth_models |
28 | | |
29 | | class User(auth_models.AbstractUser): |
30 | | class Meta: |
31 | | db_table = 'auth_user' |
32 | | }}} |
33 | | |
34 | | 5. Update the content types |
35 | | |
36 | | {{{#!python |
37 | | ./manage.py shell |
38 | | >>> from django.contrib.contenttypes.models import ContentType |
39 | | >>> ContentType.objects.filter(app_label='auth', model='user').update(app_label='user') |
40 | | }}} |
41 | | |
42 | | 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. |
43 | | |
44 | | {{{#!python |
45 | | class User(auth_models.AbstractBaseUser, |
46 | | auth_models.PermissionsMixin): |
47 | | ... |
48 | | }}} |