Opened 10 years ago
Closed 10 years ago
#25009 closed Bug (fixed)
create_user(..., is_staff=True) raises TypeError multiple values for keyword argument 'is_staff'
Reported by: | Coen van der Kamp | Owned by: | Francisco Albarran Cristobal |
---|---|---|---|
Component: | contrib.auth | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
When I do:
>>> from django.contrib.auth.models import User >>> user = User.objects.create_user('test', email='test@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
Change History (5)
comment:1 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 10 years ago
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): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if not extra_fields.get('is_staff'): raise ValueError('Superuser needs to be staff. Do not set is_staff.') if not extra_fields.get('is_superuser'): raise ValueError('Superuser needs to be superuser. Do not set is_superuser.') return self._create_user(username, email, password, **extra_fields)
comment:3 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 10 years ago
Has patch: | set |
---|
Note:
See TracTickets
for help on using tickets.
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, butUser.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: