Skip to main content

django_custom_user_migration creates migrations for you to move an existing Django project that uses django.contrib.auth.models.User to using a custom user model.

Project description

https://img.shields.io/pypi/v/django_custom_user_migration.svg

django_custom_user_migration creates migrations for you to move an existing Django project that uses django.contrib.auth.models.User to using a custom user model.

  • Free software: BSD license

Use case

You are currently using Django’s django.contrib.auth.models.User model in a deployed project, but want to migrate to a model that is under your own control, or provided by a 3rd party.

Prerequisites

  • Django 1.8 or later

  • Python 2.7 or Python 3.3+

You must have ensured that everywhere in your project (including 3rd party libraries) you are using AUTH_USER_MODEL and django.contrib.auth.get_user_model() rather than "auth.User" and django.contrib.auth.models.User.

Usage

There are a lot of steps below, but it is almost all copy/paste, and with no complications you could be done in 5 minutes.

  1. Install django_custom_user_migration to your project:

    pip install django_custom_user_migration
  2. Add "django_custom_user_migration" to your INSTALLED_APPS.

    You now have some management commands for creating migrations that we will use later.

  3. Create a custom user model which is identical to Django’s auth.User, but in an app in your own project. For this process to work correctly, you will need to create a new, dedicated app just for this model - we’ll call it accounts from now on:

    # accounts/models.py
    
    from django_custom_user_migration.models import AbstractUser
    
    class User(AbstractUser):
        pass

    The model can be called anything you want. Remember to add this app to your INSTALLED_APPS.

    Don’t add additional fields at this point, and don’t change AUTH_USER_MODEL yet.

    We avoid using django.contrib.auth.models.AbstractUser at this point, or a user model from some 3rd party library, because we get problems with related_name clashes that we can’t work around. Later on, we’ll change to inheriting from django.contrib.auth.AbstractUser, and then to another model if necessary.

  4. Create a normal migration to create the table for this:

    ./manage.py makemigrations accounts

    This migration must be 0001_initial or you will have problems later on.

  5. Create a data migration that will populate this table from auth.User:

    ./manage.py create_custom_user_populate_migration auth.User accounts.User

    All the commands to create migrations take arguments <from_model> <to_model> like this.

  6. Create a schema migration that will alter every FK that points at auth.User to point at your model instead:

    ./manage.py create_custom_user_schema_migration auth.User accounts.User
  7. Create a data migration that will fix up the contenttypes tables:

    ./manage.py create_custom_user_contenttypes_migration auth.User accounts.User
  8. Change the AbstractUser import in your models.py to:

    from django.contrib.auth.models import AbstractUser
  9. Change AUTH_USER_MODEL to "accounts.User" in your settings.

  10. Run makemigrations again:

    ./manage.py makemigrations accounts

    This creates a migration that doesn’t actually change fields, but is needed for Django to think that everything lines up again.

  11. Do related changes for admin etc. as described in Django docs: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-django-s-default-user

    Simplest version:

    # accounts/admin.py
    
    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    from . models import User
    
    admin.site.register(User, UserAdmin)
  12. Create a migration that empties the auth.User table:

    ./manage.py create_custom_user_empty_migration auth.User accounts.User
  13. Run all the migrations:

    ./manage.py migrate

    All the migrations are reversible, so you should be able to get back to previous states. Obviously, you would test this thoroughly before doing it in production.

  14. Test everything!

    Note that all migrations generated are reversible, but before running them in reverse you should set AUTH_USER_MODEL back to “auth.User”, and you will also therefore need to use the django_custom_user_migration.models.AbstractModel as a base class or you will get validation errors that prevent migrations from running.

  15. Uninstall django_custom_user_migration, you don’t need it any more. The migrations generated run without it being installed.

You can now customise your User model as required in the normal way, using migrations etc. You could even make it inherit from AbstractBaseUser or some other model instead of AbstractUser, provided that you write/generate the necessary data migrations to cope with missing fields, and update your admin and application accordingly.

Other notes

  • Use at own risk, etc. etc.

  • Tested on sqlite and postgres

  • If you have other tables with FKs to auth.User, that Django doesn’t know about, you will have to deal with those manually with a custom migration. (In really old Django projects, you might have old tables like ‘auth_message’ kicking around which you’ll need to delete).

History

0.1.0 (2015-08-14)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_custom_user_migration-0.1.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

django_custom_user_migration-0.1.0-py2.py3-none-any.whl (16.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_custom_user_migration-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_custom_user_migration-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c4fabad5f597a729182757f3984f1c489a7b7f47ca43186c86c242caf89a5dc5
MD5 6cd3b076f0e2032c4426ff278803a528
BLAKE2b-256 7293179af0213f39eba20672b987e7f1a17ca628579763921ca6ed73879ae972

See more details on using hashes here.

Provenance

File details

Details for the file django_custom_user_migration-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_custom_user_migration-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a778796129b1c1ad4d35c81d011b57991d3b17a06cafbb5d6e1bc49ad631d64
MD5 4d96ea1dfda97a5f361732924266891c
BLAKE2b-256 ee699d69eb908d98cfb469f83ee1905ebe1a1e7ff768432731e5a765ac69750b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page