Reputation: 2416
I tried to do "./manage.py syncdb", and I get prompted to create a superuser, but It fails:
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/__init__.py", line 397, in execute_from_command_line
utility.execute()
File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/__init__.py", line 390, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/base.py", line 240, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/base.py", line 283, in execute
output = self.handle(*args, **options)
File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() takes exactly 4 arguments (3 given)
Here is my userprofile:
from django.db import models
from django.conf import settings
from django.contrib.auth.models import UserManager, AbstractUser
class UserProfile(AbstractUser):
membernumber = models.CharField(max_length=20, blank=True, unique=True)
mobile = models.CharField(max_length=20, blank=True)
updated = models.DateTimeField(blank=True, null=True)
USERNAME_FIELD = 'membernumber'
objects = UserManager()
Maybe the problem is that I have run "syncdb" and having "blank=True" on the unique field?
Upvotes: 1
Views: 3707
Reputation: 346
This is clearly a bug, it needs to be fixed. Alternatively, you can create the user with python manage.py shell
from django.contrib.auth import get_user_model get_user_model().objects.create_superuser(username="admin", password="password")
Upvotes: 0
Reputation: 610
You should define your own Manager to instead of the default UserManager
;
As your example:
class MyManager(UserManager):
def create_superuser(self, membernumber,email,password,**extra_fields):
super().create_superuser(membernumber,email,password,**extra_fileds)
#or do something to save your model by yourself;
Why we need do this?
Just look your error dump
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
So when you run python manage.py createsuperuser
on your shell,after you input your membernumber
,password
,email
value,then your user_data
will become:
{'membernumber':'Garreth','password':'123456','email':'[email protected]'}
but UserManager.create_superuser
need username
param,your miss it!! So you get that error.
Here is the the source of UserManager.create_superuser
(refer 1)
def create_superuser(self, username, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(username, email, password, **extra_fields)
And some source about what run when you python manage.py createsuperuser
(refer 2)
if username:
user_data[self.UserModel.USERNAME_FIELD] = username
user_data['password'] = password
self.UserModel._default_manager.db_manager(database)
\.create_superuser(**user_data)
if options['verbosity'] >= 1:
self.stdout.write("Superuser created successfully.")
Suggestion:
If you still want to exec python manager.py createsuperuser
,you should NEVER define a custom UserModel that inherit AbstractBaseUser
,because django.contrib.admin
,django.contrib.auth
use too many AbstractUser
Model attrs like is_active
,is_superuser
,so you will get many errors when you visit yousite.com/admin
on browser.
Refers:
1.https://github.com/django/django/blob/master/django/contrib/auth/models.py#L156
Upvotes: 2
Reputation: 2247
In this book said:
Option 2: Subclass AbstractUser:
Choose this option if you like Django’s User model fields the way they are, but need extra fields.
Maybe you have to subclass AbstractBaseUser. See here or a full example.
Edit
USERNAME_FIELD
should be used if you want to use another username field. If you want that, you must subclass AbstractBaseUser
instead of AbstractUser
, because the last one already has defined a USERNAME_FIELD
Hope helps!
Upvotes: 0