allow custom user models in rename_groups migration#59
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed a bug in the
rename_groupsmigration that prevented projects using custom User models from running migrations. The migration was originally hardcoded to use django's defaultauth.Usermodel which caused anAttributeErrorwhenAUTH_USER_MODELwas set to a custom model.When a project defined a custom User model via
AUTH_USER_MODEL, the migration would crash withThis occurred because the migration directly referenced
apps.get_model("auth", "User")instead of using theAUTH_USER_MODELsetting. This means any project using a custom User model could not run migrations.As a solution, we updated
accounts/migrations/rename_groups.pyto properly support both default and custom User models:Use AUTH_USER_MODEL setting
Add swappable dependency
This works because:
settings.AUTH_USER_MODELreturns"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 itThe changes are also fully backwards compatible:
AUTH_USER_MODELis not set, it defaults to"auth.User"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_MODELsetting (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