首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >定制django用户模型失败

定制django用户模型失败
EN

Stack Overflow用户
提问于 2018-11-06 13:00:45
回答 3查看 773关注 0票数 0

**嗨,我正在尝试自定义django默认用户。应用迁移很好。但是,当我试图创建一个超级用户时,我会得到一个错误。你能告诉我为什么会有这个错误吗?

* self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) "/home/gravityns/PycharmProjects/dev/shop/models.py",“/home/gravityns/PycharmProjects/dev/shop/models.py”,第54行,在create_superuser user.is_staff = True AttributeError:无法设置属性

代码语言:javascript
运行
复制
# accounts.models.py

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)
from django.core.validators import RegexValidator


class UserManager(BaseUserManager):
    def create_user(self, username, password, email):
        """
        Creates and saves a User with the given email and password.
        """
        if not username:
            raise ValueError('Users must have a username')
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            username = username,
            email = self.normalize_email(email),

        )

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

    def create_staffuser(self, username, password, email):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            username,
            email,
            password,

        )
        user.is_staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, username, password, email):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            username,
            email,
            password

        )
        user.is_staff = True
        user.is_admin = True
        user.save(using=self._db)
        return user


USERNAME_REGEX = '^[a-zA-Z0-9.@+-]*$'

class User(AbstractBaseUser):
    username = models.CharField(max_length=255, validators=[
        RegexValidator(regex= USERNAME_REGEX,
                       message = 'Username must be Alphanumeric or any of the following: ". @ + -"')],
                                unique=True

                                )
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    created_at = models.DateField(auto_now_add=True, blank=True)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    is_staff = models.BooleanField(default=False) # a admin user; non super-user

    # notice the absence of a "Password field", that's built in.

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email'] # Email & Password are required by default.

    objects = UserManager()

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.is_staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.is_admin

    @property
    def is_active(self):
        "Is the user active?"
        return self.is_active
EN

Stack Overflow用户

发布于 2018-11-06 13:20:41

用户模型上有一个作为User.is_staff的字段和一个同名的@property。因此,您不能通过user_instance.is_staff = True设置模型对象的属性。

您可以将is_staff作为字段或类属性,而不是两者兼而有之。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53172431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档