Skip to content
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

admin_user doesn't like django_username_field being set to email #493

Closed
tolomea opened this issue Jun 18, 2017 · 6 comments
Closed

admin_user doesn't like django_username_field being set to email #493

tolomea opened this issue Jun 18, 2017 · 6 comments

Comments

@tolomea
Copy link

tolomea commented Jun 18, 2017

I'm using https://github.com/tmm/django-username-email which uses the email in place of having a separate user name, this generally works well but it breaks the admin_user fixture because this:

if username_field != 'username':
    extra_fields[username_field] = 'admin'

leads to this

user = UserModel._default_manager.create_superuser(
    'admin', 'admin@example.com', 'password', **extra_fields)

raising this

TypeError: create_superuser() got multiple values for argument 'email'
@alexdemari
Copy link

I have the same problem.

@aalvrz
Copy link

aalvrz commented Dec 28, 2017

Did you find a solution for this?

@tolomea
Copy link
Author

tolomea commented Jan 8, 2018

I overrode the admin_user fixture in conftest.py, you just declare a function there with the same name. Mine looks like:

@pytest.fixture()
def admin_user(db, django_user_model, django_username_field):
    """A Django admin user.

    This uses an existing user with username 'admin', or creates a new one with
    password 'password'.
    """
    UserModel = django_user_model
    username_field = django_username_field

    try:
        user = UserModel._default_manager.get(**{username_field: 'admin@example.com'})
    except UserModel.DoesNotExist:
        extra_fields = {}
        user = UserModel._default_manager.create_superuser(
            'admin@example.com', 'password', **extra_fields)
    return user

@driesdesmet
Copy link

Yeah, I also overwrote it. Easy enough to do. It looks like the authors of pytest-django took the effort of making it work for the case where username=email, but then in practice, this doesn't work.

@blueyed
Copy link
Contributor

blueyed commented Apr 23, 2019

The code was changed in this regard: https://github.com/blueyed/pytest-django/blob/33617687f185fecf04bf4bdd99017289078f52df/pytest_django/fixtures.py#L261-L262

Via a3dc56d

So I assume this (or at least the original issue) is fixed?

@blueyed blueyed closed this as completed Apr 23, 2019
@driesdesmet
Copy link

In my case I actually had an adapted UserModel._default_manager.create_superuser method that does not take a username argument. That's what you tend to do if you have a custom UserModel that does not have a username field (but an email field).

So, your code change is indeed better, but does not cover the case when you have no username field. Which is moot anyway, because you can't cover all custom cases of course. Thanks for the follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants