Skip to content

Commit bb9e86e

Browse files
authored
admin_user - get user by natural key (#879)
1 parent 1dd31b2 commit bb9e86e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pytest_django/fixtures.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ def admin_user(db, django_user_model, django_username_field):
287287
username = "admin@example.com" if username_field == "email" else "admin"
288288

289289
try:
290-
user = UserModel._default_manager.get(**{username_field: username})
290+
# The default behavior of `get_by_natural_key()` is to look up by `username_field`.
291+
# However the user model is free to override it with any sort of custom behavior.
292+
# The Django authentication backend already assumes the lookup is by username,
293+
# so we can assume so as well.
294+
user = UserModel._default_manager.get_by_natural_key(username)
291295
except UserModel.DoesNotExist:
292296
extra_fields = {}
293297
if username_field not in ("username", "email"):

0 commit comments

Comments
 (0)