TypeError: create_superuser() missing 2 required positional arguments
TypeError: create_superuser() missing 2 required positional arguments
我正在尝试创建超级用户
当我输入电子邮件和密码时出现此错误
File "/mnt/c/Users/ZAKARIA/Desktop/project/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 232, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(
TypeError: create_superuser() missing 2 required positional arguments: 'first_name' and 'last_name'
我的 class 经理
from django.contrib.auth.base_user import BaseUserManager
class UserManger(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError("Users must have an email address")
if not first_name or last_name:
raise ValueError("Users must have a first and last name")
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email,password, first_name, last_name):
user = self.create_user(
email=self.normalize_email(email),
password=password,
first_name=first_name,
last_name=last_name,
)
user.set_password(user.password)
#user.is_staff = True
user.is_superuser = True
#user.is_admin = True
user.save(using=self._db)
return user
型号
import datetime
from django.core.mail import send_mail
from distutils.command.upload import upload
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils import timezone
from phonenumber_field.modelfields import PhoneNumberField
from .managers import UserManger
GENDER_MALE = "m"
GENDER_FEMALE = "f"
OTHER = "o"
GENDER_CHOICES = (
(GENDER_MALE, "Male"),
(GENDER_FEMALE, "Female"),
(OTHER, "Other"),
)
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
is_active = models.BooleanField(default=True)
#is_staff = models.BooleanField(default=False)
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
def get_full_name(self):
full_name = '%S %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def __str__(self):
return self.email
def has_perm(self, prem, obj=None):
"Does the user have a specific permission?"
return True
def has_module_perm(self, app_label):
"Does the user have permissions to view the app `app_label`?"
return True
''''
@property
def is_staff(self):
"Is the user a member of staff"
return self.is_admin'''
"""@property
def is_out_of_credits(self):
"Is the user out of credits"
return self.credits > 0
@property
def has_sufficient_credits(self,cost):
return self.credits - cost >= 0
"""
@property
def linkedin_signed_in(self):
return bool(self.linkedin_token) and self.expiry_date > timezone.now()
看到很多类似的问题,但我还是不明白给出的答案,有人说添加立场争论,我不太明白
can any one help me to solve this problem ?
这可能是因为您在 UserModel 中有一些额外的字段没有在 create_superuser
中准确传递。
尝试 **extra_fields
arg 而不是分别传递 first_name、last_name,正如关于 Manager methods 的文档所建议的那样,
The extra_fields keyword arguments are passed through to the User’s
init method to allow setting arbitrary fields on a custom user model.
所以,根据这个修改你的UserManger
class,这样就可以解决问题了。
修改后的代码:
class UserManger(BaseUserManager):
"""Define a model manager for User model"""
def _create_user(self, email, password=None, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
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(email, password, **extra_fields)
现在你可以将first_name
、last_name
设置为UserModel
中的REQUIRED_FIELDS
,像这样。
REQUIRED_FIELDS = ['first_name' , 'last_name']
有关BaseUserManager
的详细信息,您还可以查看Docs
编辑:
在看到您的 UserModel 之后,使用 AbstractUser 而不是 AbstractBaseUser 会更好,AbstractUser 将携带默认 Django 用户具有的所有字段。因此,您不必定义 first_name、last_name、is_staff 等字段...
这就是你可以修改它的方式
# import
from django.contrib.auth.models import AbstractUser
# Model
class User(AbstractUser):
username = None # as you are using email as username
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
# Do anything else here
我正在尝试创建超级用户 当我输入电子邮件和密码时出现此错误
File "/mnt/c/Users/ZAKARIA/Desktop/project/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 232, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(
TypeError: create_superuser() missing 2 required positional arguments: 'first_name' and 'last_name'
我的 class 经理
from django.contrib.auth.base_user import BaseUserManager
class UserManger(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError("Users must have an email address")
if not first_name or last_name:
raise ValueError("Users must have a first and last name")
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email,password, first_name, last_name):
user = self.create_user(
email=self.normalize_email(email),
password=password,
first_name=first_name,
last_name=last_name,
)
user.set_password(user.password)
#user.is_staff = True
user.is_superuser = True
#user.is_admin = True
user.save(using=self._db)
return user
型号
import datetime
from django.core.mail import send_mail
from distutils.command.upload import upload
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils import timezone
from phonenumber_field.modelfields import PhoneNumberField
from .managers import UserManger
GENDER_MALE = "m"
GENDER_FEMALE = "f"
OTHER = "o"
GENDER_CHOICES = (
(GENDER_MALE, "Male"),
(GENDER_FEMALE, "Female"),
(OTHER, "Other"),
)
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
is_active = models.BooleanField(default=True)
#is_staff = models.BooleanField(default=False)
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
def get_full_name(self):
full_name = '%S %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def __str__(self):
return self.email
def has_perm(self, prem, obj=None):
"Does the user have a specific permission?"
return True
def has_module_perm(self, app_label):
"Does the user have permissions to view the app `app_label`?"
return True
''''
@property
def is_staff(self):
"Is the user a member of staff"
return self.is_admin'''
"""@property
def is_out_of_credits(self):
"Is the user out of credits"
return self.credits > 0
@property
def has_sufficient_credits(self,cost):
return self.credits - cost >= 0
"""
@property
def linkedin_signed_in(self):
return bool(self.linkedin_token) and self.expiry_date > timezone.now()
看到很多类似的问题,但我还是不明白给出的答案,有人说添加立场争论,我不太明白
can any one help me to solve this problem ?
这可能是因为您在 UserModel 中有一些额外的字段没有在 create_superuser
中准确传递。
尝试 **extra_fields
arg 而不是分别传递 first_name、last_name,正如关于 Manager methods 的文档所建议的那样,
The extra_fields keyword arguments are passed through to the User’s init method to allow setting arbitrary fields on a custom user model.
所以,根据这个修改你的UserManger
class,这样就可以解决问题了。
修改后的代码:
class UserManger(BaseUserManager):
"""Define a model manager for User model"""
def _create_user(self, email, password=None, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
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(email, password, **extra_fields)
现在你可以将first_name
、last_name
设置为UserModel
中的REQUIRED_FIELDS
,像这样。
REQUIRED_FIELDS = ['first_name' , 'last_name']
有关BaseUserManager
的详细信息,您还可以查看Docs
编辑: 在看到您的 UserModel 之后,使用 AbstractUser 而不是 AbstractBaseUser 会更好,AbstractUser 将携带默认 Django 用户具有的所有字段。因此,您不必定义 first_name、last_name、is_staff 等字段...
这就是你可以修改它的方式
# import
from django.contrib.auth.models import AbstractUser
# Model
class User(AbstractUser):
username = None # as you are using email as username
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
# Do anything else here