#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 Thomas Grainger):

 I've used a custom management command to automate this - avoiding raw sql
 by using MigrationRecorder

 {{{
 from __future__ import annotations

 import argparse

 from django.contrib.contenttypes.models import ContentType
 from django.core.management import call_command
 from django.core.management.base import BaseCommand
 from django.db import DEFAULT_DB_ALIAS, connections
 from django.db.migrations.recorder import MigrationRecorder


 class Command(BaseCommand):
     """
     Custom management command for forcing the django custom user migration
     """

     help = "fake users.migrations.0001_initial and switch auth.User
 content types to users.User if required"

     def add_arguments(self, parser: argparse.ArgumentParser) -> None:
         parser.add_argument(
             "--database",
             default=DEFAULT_DB_ALIAS,
             help="Nominates a database to apply migrations for. Defaults
 to the 'default' database.",
         )

     def handle(self, *args: object, database: object = DEFAULT_DB_ALIAS,
 **options: object) -> None:
         """
         fake users.migrations.0001_initial and switch auth.User content
 types to users.User if required"
         """
         assert isinstance(database, str)
         connection = connections[database]
         recorder = MigrationRecorder(connection)
         recorded_migrations = recorder.applied_migrations()

         if (
             # not faked yet
             ("users", "0001_initial") not in recorded_migrations
             # django auth migrations have already been applied
             and ("auth", "0001_initial") in recorded_migrations
         ):
             # https://www.caktusgroup.com/blog/2019/04/26/how-switch-
 custom-django-user-model-mid-project/
             recorder.record_applied(app="users", name="0001_initial")
             ContentType.objects.filter(app_label="auth",
 model="user").update(app_label="users")

         call_command("migrate", database=database)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25313#comment:26>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.e081cc5da3ada0851f4c540b09b667c2%40djangoproject.com.

Reply via email to