TypeError:create_superuser()缺少3个必需的位置参数:

时间:2019-03-19 11:00:49

标签: python django typeerror django-custom-user django-custom-manager

尝试使用终端创建新的超级用户时遇到以下错误。

TypeError: create_superuser() missing 3 required positional arguments: 'first_name', 'last_name', and 'location'

我在下面的评论中关注了另一个stackoverflow页面。在该页面中,它坚持使用user.save(using=self._db)创建一个create_superuser函数。 这段代码仍然有错误,

如何使用“ first_name”,“ last_name”和“ location”而不提供任何默认值。

first_name,last_name和location是CharField,其max_length = 30,并且可以具有空白值。

在此自定义用户模型中,通过在User类中将用户名设置为唯一,将其替换为mobile_no。

models.py

from django.db import models
from django.contrib.auth.models import (
        AbstractBaseUser, BaseUserManager,PermissionsMixin
    )
class UserManager(BaseUserManager):
    # All required field must be passed below as argument
    def create_user(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False):
        if not mobile_no:
            raise ValueError("User must have an Mobile number as username ")
        if not password:
            raise ValueError("Users must have a password")

        user_obj= self.model(
            mobile_no= mobile_no
        )
        user_obj.email = email
        user_obj.set_password(password)
        user_obj.role = role
        user_obj.first_name = first_name
        user_obj.las_name = last_name
        user_obj.is_active = is_active
        user_obj.location = location
        user_obj.is_staff = is_staff
        user_obj.save(using=self._db)
        return user_obj

    def create_superuser(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False):
        user_obj= self.model(
            mobile_no=mobile_no
        )
        user_obj.email = email
        user_obj.set_password(password)
        user_obj.role = role
        user_obj.first_name = first_name
        user_obj.las_name = last_name
        user_obj.location = location
        user_obj.is_staff = True
        user_obj.is_admin = True
        user_obj.is_active = is_active
        user_obj.save(using=self._db)
        return user_obj

    def create_staffuser(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False):
        user_obj= self.model(
            mobile_no=mobile_no
        )
        user_obj.email = email
        user_obj.set_password(password)
        user_obj.role = role
        user_obj.first_name = first_name
        user_obj.las_name = last_name
        user_obj.location = location
        user_obj.is_staff = True
        user_obj.is_admin = False
        user_obj.is_active = is_active
        user_obj.save(using=self._db)
        return user_obj


    # def get_by_natural_key(self, mobile_no_):
    #     print(mobile_no_)
    #     return self.get(mobile_no=mobile_no_)

class User(AbstractBaseUser, PermissionsMixin):
    mobile_no   = models.CharField(max_length=10,unique=True)
    email       = models.EmailField(max_length=255)
    role        = models.CharField(max_length=255)
    first_name  = models.CharField(max_length=30, blank=True)
    last_name   = models.CharField(max_length=30, blank=True)
    location    = models.CharField(max_length=30, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active   = models.BooleanField(default=True)
    is_staff    = models.BooleanField(default=False)
    is_admin    = models.BooleanField(default=False)

    USERNAME_FIELD = 'mobile_no'
    REQUIRED_FIELDS = ['email', 'role']

    objects= UserManager()

    def get_full_name(self):
        return self.first_name + self.last_name

    def get_short_name(self):
        return self.mobile_no

    def __str__(self):
        return self.mobile_no

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin

    @property
    def is_active(self):
        return self.active

2 个答案:

答案 0 :(得分:1)

此代码适用于上述情况

class UserManager(BaseUserManager):
    def create_user(self, mobile_no, role, password=None):
        """
        Creates and saves a User with the given email and password.
        """
        if not mobile_no:
            raise ValueError('Users must have an Mobile No')

        user = self.model(
            mobile_no=mobile_no,
        )

        user.set_password(password)
        user.activated=True
        user.save(using=self._db)
        return user

    def create_superuser(self, mobile_no, role, password):
        """
         Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(mobile_no, role=role,
                                password=password
                                )
        user.is_admin = True
        user.activated=True
        user.save(using=self._db)
        return user

    def create_staffuser(self, mobile_no, role, password):
        """
         Creates and saves a staffuser with the given email and password.
        """
        user = self.create_user(mobile_no, role=role,
                                password=password
                                )
        user.is_staff = True
        user.activated=True
        user.save(using=self._db)
        return user
class Users(AbstractBaseUser, PermissionsMixin):
    object = UserManager()
    mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True)
    email = models.EmailField(_('Email'), max_length=75, null=False, blank=False)
    first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True)
    last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True)
    role = models.CharField(_('Role'), max_length=70, null=True, blank=True)
    location = models.CharField(_('Location'), max_length=70, null=True, blank=True)
    date_time = models.DateTimeField(_('DateTime'), auto_now=True, null=True, blank=True)
    activated = models.BooleanField(_('Activated'), default=False)
    is_admin = models.BooleanField(_('is_admin'), default=False)
    is_staff = models.BooleanField(_('is_staff'), default=False)

    def __unicode__(self):
        return str(self.mobile_no)

    def __str__(self):
        return str(self.mobile_no)

    def get_full_name(self):
        return self.first_name + " " + self.last_name

    class Meta:
        ordering = ['-id']

    @property
    def is_staff(self):
        return self.is_admin


    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return self.is_admin

    USERNAME_FIELD = 'mobile_no'
    REQUIRED_FIELDS = ['role']

答案 1 :(得分:0)

您可能会遇到此问题,因为您在必填字段中仅指定了电子邮件和角色,而没有first_name,last_name和位置。在声明用户用户管理器时,没有为这些字段提供默认值,因此必须在必填字段中指定它们。那应该可以解决您的问题。

此答案的功劳归@Daniel Roseman:Issue with createsuperuser when implementing custom user model