TypeError:MyUser()得到了一个意外的关键字参数“is_superuser - Django REST

ql3eal8s  于 2023-03-20  发布在  Go
关注(0)|答案(2)|浏览(162)

我正在使用Django REST Framework作为我的REST API。
我正在创建一个自定义用户模型。下面是我的代码:

from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import UserManager
from django.db import models
import time

# Create your models here.
class MyUserManager(BaseUserManager):
    def create_user(self, first_name, last_name, email, password, **kwargs):
        if(not email):
           raise ValueError("No Email Provided!")

        email = self.normalize_email(email)
        user = self.model(email, **kwargs)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, first_name, last_name, email, password, **kwargs ):
        kwargs.setdefault('is_staff', True)
        kwargs.setdefault('is_superuser', True)
        kwargs.setdefault('is_active', True)

        if(kwargs.get('is_staff') is not True):
           raise ValueError("Super User must be a staff in order to be in!")

        if(kwargs.get('is_superuser') is not True):
           raise ValueError("User must be a SuperUser!")

        if(kwargs.get('is_active') is not True):
           raise ValueError("Super User must be active!")

        return self.create_user(first_name, last_name, email, password, **kwargs)


class MyUser(AbstractBaseUser):
    first_name = models.CharField(max_length= 200, blank= False)
    last_name = models.CharField(max_length= 200, blank= False)
    email = models.EmailField(unique= True, blank= False)
    phone_number = models.IntegerField()
    company_name = models.CharField(max_length= 200, blank= False)
    date_joined = models.DateTimeField(auto_now= True)
    last_login = models.DateTimeField(auto_now= True, null= False)
    is_staff = models.BooleanField(default= False)
    is_active = models.BooleanField(default= True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'phone_number', 'company_name']

    objects = MyUserManager()

下面是我得到的错误:

File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)

File "C:\Users\Desktop\Auth-App\AuthProject\AuthApplication\models.py", line 33, in create_superuser
return self.create_user(first_name, last_name, email, password, **kwargs)

File "C:\Users\Desktop\Auth-App\AuthProject\AuthApplication\models.py", line 13, in create_user
user = self.model(email, **kwargs)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\base.py", line 500, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))

TypeError:MyUser()获得意外的关键字参数“is_superuser”

上述问题的可能解决方案是什么?

cyvaqqii

cyvaqqii1#

应该从AbstractUser继承MyUser,而不是从AbstractBaseUser继承。实际上,is_superuser是在PermissionMixin中设置的,由AbstractUser使用。在AbstractBaseUser中,不存在这个必要的var。
因此,解决方案很简单:

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
ctehm74n

ctehm74n2#

您可以使用AbstractBaseUser,但还需要扩展PermissionsMixin,因为is_superuser是在PermissionsMixin中定义的。
PermissionsMixin是设置组和权限模型的地方。
您的MyUser类应类似于以下内容:

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin

class User(AbstractBaseUser, PermissionsMixin):
    ...

以下是有用的教程/指南:link .

相关问题