-
Notifications
You must be signed in to change notification settings - Fork 348
Make admin_user & admin_client fixtures compatible with custom user models #843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
d1b4335
to
dcf4202
Compare
The previous test setup inherited from AbstractUser thus MyCustomUser still had a username field. The problems people are having when using the admin_client fixture in combination with a custom user model are due to the username field not being present. This change accounts for the more realistic scenario. See these tickets: pytest-dev#246 pytest-dev#484 pytest-dev#748
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. See this ticket: pytest-dev#457
"1 passed, 1 warning" wasn't matched before when there was a deprecation warning. Newer Django versions warn about the removal of `django.conf.urls.url` in favor of `django.urls.re_path` but Django 1.11 is also tested against and doesn't support `re_path` yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jnns! This looks good to me and fixes what seems to be a long-standing issue.
If you could do the following that would be great:
- Fix my one tiny comment.
- Rebase the PR on latest master.
- Add an entry to the changelog (
docs/changelog.rst
). - Edit the PR to say "Closes #XXX" on old the old PRs/issues that this fixes, so they're automatically closed once merged.
username_field: username, | ||
"email": "admin@example.com", | ||
"password": "password", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case username_field
is email, it should override the email
below, so should come after it (even though the values are the same, but just for good measure).
username_field: username, | |
"email": "admin@example.com", | |
"password": "password", | |
"email": "admin@example.com", | |
"password": "password", | |
username_field: username, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to review! I will add the requested changes and push again.
Using the
admin_client
fixture with a custom user model that doesn't have a username field doesn't work currently. The fixture assumes that the user model always has ausername
field present. For this case, Django hasUser.USERNAME_FIELD
andUser.get_username()
.Unfortunately, the existing test didn't catch this issue because a custom user model with
User.username
is used.This pull request changes the test setup to use a custom user without
username
and makesadmin_client
useUser.get_username()
.There are a few merge requests and issues about this problem already. I hope this one meets the requirements to be merged. If not, please let me know and I see what I can do.