Changes between Initial Version and Version 1 of Ticket #25313, comment 24
- Timestamp:
- Apr 12, 2021, 3:39:48 PM (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #25313, comment 24
initial v1 30 30 }}} 31 31 32 4. Edit the migration with a name starting with "0001" of this app. 32 4. Set this new model as the default user model in the settings.py file by adding: `AUTH_USER_MODEL = "user.User"`. 33 34 5. Edit the migration with a name starting with "0001" of this app. 33 35 34 36 Under "operations" paste the following as the first item: … … 74 76 (The exact code to use here will likely change over time with newer versions of Django. You can find the current code by creating a new app temporarily, add the User model to it and then look at the migration file `./manage.py makemigrations` produces.) 75 77 76 5. Create a new data migration to the user app by typing: `./manage.py makemigrations --empty user`78 6. Create a new data migration to the user app by typing: `./manage.py makemigrations --empty user` 77 79 78 6. Edit the newly created migration:80 7. Edit the newly created migration: 79 81 80 82 Under "operations" add `migrations.RunPython(change_user_type),` … … 94 96 }}} 95 97 96 7. You can now migrate to the custom user model by running `./manage.py migrate`. This should always work, however, it will do so in two different ways depending on whether it is run on an existing or a new instance:98 8. You can now migrate to the custom user model by running `./manage.py migrate`. This should always work, however, it will do so in two different ways depending on whether it is run on an existing or a new instance: 97 99 98 100 * On a new instance, the new user model will be created in migration 0001. The last migration will have no effect. … … 100 102 * On an existing instance, the migration 0001 will be ignored as the system has already previously applied migration 0001 and will therefore now ignore it. Instead the last migration will have the effect of change the app of the user model. 101 103 102 8. Now, you should be able to make changes to your users.User model and run makemigrations / migrate as needed. For example, as a first step, you may wish to rename the auth_user table to something in your users app's namespace. You can do so by removing db_table from your User model, so it looks like this:104 9. Now, you should be able to make changes to your users.User model and run makemigrations / migrate as needed. For example, as a first step, you may wish to rename the auth_user table to something in your users app's namespace. You can do so by removing db_table from your User model, so it looks like this: 103 105 104 106 {{{ … … 116 118 Please let me know if this works for you - especially with other databases. 117 119 118 [1] https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/ with mo120 [1] https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/