2👍
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':'like@gamil.com'}
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
0👍
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!
- [Answered ]-Jquery function cannot send ajax request to django server
- [Answered ]-Passing Model Field Data to Validators in Django
- [Answered ]-How to use vagrant to develop on django locally and then deploy to EC2/Azure?
0👍
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")
- [Answered ]-Django Rest Framework: Order by user dependent field
- [Answered ]-Simplify django init on getting model object
- [Answered ]-Internal url redirection in django
- [Answered ]-Minimal developer setup of sorl-thumbnail with Django 1.7
- [Answered ]-How to set django many-to-many field to accept null