- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用了 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/
我使用了 Django restframework。 为了实现自定义用户模型,我使用了 AbstractBaseUser。 models.py 代码如下。 [模型.py] from django.db
如果用户是 super 用户,我想显示一个按钮。我找到了不同的示例,但我的代码不起作用。按钮不出现。有人知道为什么吗? View .py def inici(request): zones =
如果用户不是 is_staff/is_superuser,则一直在尝试确定从 from 中删除字段的“最”优雅的解决方案。找到了一个有效的方法,代码量最少。最初我想在“排除”元中添加“关闭”或使用两种
我有一个自定义用户模型 class User(AbstractBaseUser, PermissionsMixin): ... 字段is_superuser继承自PermissionsMixi
我正在尝试使用此代码更新用户密码: from django.contrib.auth.models import User u = User.objects.get(username='test')
您好,我正在使用一个模板,我想在其中显示或隐藏 html block ,具体取决于用户是否是 super 用户。代码如下: What user.is_superuser returns: {{ us
我已经为此纠结了几天了。 django.contrib.auth.models 有一个允许多个权限的用户模型,但有 is_staff、is_superuser 和 is_active 的 3 个标志。
我是一名优秀的程序员,十分优秀!