gpt4 book ai didi

django - “is_superuser”是此函数的无效关键字参数

转载 作者:行者123 更新时间:2023-12-01 12:11:59 25 4
gpt4 key购买 nike

我使用了 Django restframework。

为了实现自定义用户模型,我使用了 AbstractBaseUser。

models.py 代码如下。

[模型.py]

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.utils import timezone

class UserManager(BaseUserManager):
use_in_migrations = True

def _create_user(self, username, email, password, is_staff, is_admin, is_active, is_superuser, **extra_fields):
now = timezone.now()
if not username:
raise ValueError('Username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_admin=is_admin,
is_active=is_active, is_superuser=is_superuser,
date_joined=now, **extra_fields)

user.set_password(password)
user.save(self._db)
return user

def create_user(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, False, False, True, False, **extra_fields)

def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True, True, True, **extra_fields)

class User(AbstractBaseUser):
USER_TYPE_CHOICES = (
('django', 'Django'),
('facebook', 'Facebook'),
('google', 'Google')
)

user_type = models.CharField(
max_length=20,
choices=USER_TYPE_CHOICES,
default='Django'
)
email = models.CharField(max_length=100, null=False)
username = models.CharField(max_length=20, unique=True)
phone = models.CharField(max_length=12)

# Default Permission
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)

date_joined = models.DateTimeField(auto_now_add=True)

USERNAME_FIELD = 'username'
objects = UserManager()
REQUIRED_FIELDS = ['email']

def get_full_name(self):
pass

def get_short_name(self):
pass

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

@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

@is_staff.setter
def is_staff(self, value):
self._is_staff = value

当我创建 super 用户时,

它抛出 TypeError: 'is_superuser' is an invalid keyword argument for this function

也许我认为我的代码中没有与 is_superuser 相关的功能,但我不知道我必须做什么。

有什么解决办法吗?

谢谢。

最佳答案

看起来 is_superuser 字段被同名的属性覆盖了。您应该重命名 is_superuser 属性以修复错误:

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

关于django - “is_superuser”是此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51098649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com