From 387946da8cdb275d450918905fdfd268bd96471e Mon Sep 17 00:00:00 2001 From: olzhasar Date: Fri, 9 Oct 2020 16:26:33 +0600 Subject: [PATCH 1/2] admin_user - get user by natural key --- pytest_django/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 0f2dd6115..4d6a197b0 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -287,7 +287,7 @@ def admin_user(db, django_user_model, django_username_field): username = "admin@example.com" if username_field == "email" else "admin" try: - user = UserModel._default_manager.get(**{username_field: username}) + user = UserModel._default_manager.get_by_natural_key(username) except UserModel.DoesNotExist: extra_fields = {} if username_field not in ("username", "email"): From 776b0b2cca41302567488f888b32ec4d174cc575 Mon Sep 17 00:00:00 2001 From: Olzhas Arystanov Date: Fri, 9 Oct 2020 18:03:28 +0600 Subject: [PATCH 2/2] Update pytest_django/fixtures.py Co-authored-by: Ran Benita --- pytest_django/fixtures.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 4d6a197b0..3a840a57a 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -287,6 +287,10 @@ def admin_user(db, django_user_model, django_username_field): username = "admin@example.com" if username_field == "email" else "admin" try: + # The default behavior of `get_by_natural_key()` is to look up by `username_field`. + # However the user model is free to override it with any sort of custom behavior. + # The Django authentication backend already assumes the lookup is by username, + # so we can assume so as well. user = UserModel._default_manager.get_by_natural_key(username) except UserModel.DoesNotExist: extra_fields = {}