{{{
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('test', email='te...@example.com',
password='password', is_staff=True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/coen/git/prive/tvdordrecht.nl/env/lib/python2.7/site-
packages/django/contrib/auth/models.py", line 183, in create_user
**extra_fields)
TypeError: _create_user() got multiple values for keyword argument
'is_staff'
}}}
Expected:
{{{
>>> user.is_staff
True
}}}
From StackOverflow:
http://stackoverflow.com/questions/30711544/create-staff-user-in-
django/30946633#30946633
Related
https://github.com/bak1an/django/commit/dc2f67679712412e2f8bdfbecc340f796d6dbc24
https://code.djangoproject.com/ticket/20541
--
Ticket URL: <https://code.djangoproject.com/ticket/25009>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
I can indeed reproduce the issue.
Interestingly, prior to cab333cb16656cbb7d2bcfb06b2f7aeab9bac7af (which
landed in 1.6), doing `User.objects.create_user(..., is_staff=True)`
actually worked, but `User.objects.create_superuser(..., is_staff=False)`
would raise a similar error to what we have now.
I think the fix should be to allow these parameters to be passed. It can
lead to weird code like `User.objects.create_superuser(...,
is_superuser=False)` but I don't think we should code against that.
The fix is then quite straighforward:
{{{#!python
def create_user(self, username, email=None, password=None,
**extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(username, email, password,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(username, email, password,
**extra_fields)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25009#comment:1>
Comment (by allcaps):
Seems like we can have a cheeseburger without cheese :). Forgiveness is
cool. But we can also raise an error for the superuser.
{{{
def create_user(self, username, email=None, password=None,
**extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(username, email, password, **extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
if extra_fields.has_key('is_staff') and extra_fields.get('is_staff')
is False:
raise ValueError('Superuser needs to be staff. Do not set
`is_staff`.')
if extra_fields.has_key('is_superuser') and
extra_fields.get('is_superuser') is False:
raise ValueError('Superuser needs to be superuser. Do not set
`is_superuser`.')
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(username, email, password, **extra_fields)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25009#comment:2>
* owner: nobody => pahko
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/25009#comment:3>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25009#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"e75b614640c206bf6d0b1c9d32c54434ea719582" e75b6146]:
{{{
#!CommitTicketReference repository=""
revision="e75b614640c206bf6d0b1c9d32c54434ea719582"
Fixed #25009 -- Allowed User.objects.create_user(...,is_staff=True) to
work.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25009#comment:5>