Skip to content

allow custom user models in rename_groups migration#59

Merged
ryantanen merged 3 commits intomasterfrom
james/support-custom-user-models
Jan 16, 2026
Merged

allow custom user models in rename_groups migration#59
ryantanen merged 3 commits intomasterfrom
james/support-custom-user-models

Conversation

@jamesdoh0109
Copy link
Contributor

@jamesdoh0109 jamesdoh0109 commented Dec 27, 2025

Fixed a bug in the rename_groups migration that prevented projects using custom User models from running migrations. The migration was originally hardcoded to use django's default auth.User model which caused an AttributeError when AUTH_USER_MODEL was set to a custom model.

When a project defined a custom User model via AUTH_USER_MODEL, the migration would crash with

AttributeError: Manager isn't available; 'auth.User' has been swapped for 'myapp.User'

This occurred because the migration directly referenced apps.get_model("auth", "User") instead of using the AUTH_USER_MODEL setting. This means any project using a custom User model could not run migrations.

As a solution, we updated accounts/migrations/rename_groups.py to properly support both default and custom User models:

Use AUTH_USER_MODEL setting

   # Before
   User = apps.get_model("auth", "User")
   
   # After  
   User = apps.get_model(settings.AUTH_USER_MODEL)

Add swappable dependency

   dependencies = [
       ("accounts", "0001_initial"),
       migrations.swappable_dependency(settings.AUTH_USER_MODEL),  # Added
   ]

This works because:

  • settings.AUTH_USER_MODEL returns "auth.User" by default or the custom model path (e.g., "myapp.User")
  • apps.get_model() accepts both formats: "app.Model" and ("app", "Model")
  • swappable_dependency() ensures the migration runs after the User model is created, regardless of which app defines it

The changes are also fully backwards compatible:

  • Projects using Django's default User model see identical behavior
  • When AUTH_USER_MODEL is not set, it defaults to "auth.User"
  • The migration ordering is unchanged for default User configurations
  • No data migrations or database changes required

Tests:
Added test coverage in accounts/tests/test_migrations.py:
✅ Groups renamed correctly with platform_ prefix
✅ User group memberships updated properly
✅ All required platform groups created
✅ Migration uses AUTH_USER_MODEL setting (core fix)
✅ Handles users without platform groups
✅ Idempotent (safe to run multiple times)
✅ Works with empty database
✅ Compatible with both dotted string and split formats

@ryantanen ryantanen self-requested a review January 16, 2026 22:00
Copy link
Contributor

@ryantanen ryantanen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ryantanen ryantanen merged commit 897c474 into master Jan 16, 2026
3 checks passed
@ryantanen ryantanen deleted the james/support-custom-user-models branch January 16, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants