Skip to content

Commit

Permalink
Fix admin_client & admin_user for custom user model
Browse files Browse the repository at this point in the history
Relying on `User.USERNAME_FIELD` is enough for light customization and
doesn't require `extra_arguments`.

Esoteric user models probably need a specially tailored manager anyway
and most likely a custom `django_user` fixture that goes with that.
  • Loading branch information
jnns authored and bluetech committed Oct 16, 2020
1 parent a079cd6 commit 503ef3e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ def admin_user(db, django_user_model, django_username_field):
# so we can assume so as well.
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
extra_fields = {}
if username_field not in ("username", "email"):
extra_fields[username_field] = "admin"
user = UserModel._default_manager.create_superuser(
username, "admin@example.com", "password", **extra_fields
**{
"email": "admin@example.com",
"password": "password",
username_field: username,
}
)
return user

Expand Down

0 comments on commit 503ef3e

Please sign in to comment.