Django:AbstractBaseUser和Error =' AttributeError:无法设置属性'

时间:2015-09-18 05:55:56

标签: python django

我是django的新手。我编写了我的django用户模型和用户管理器,如下所示:

的UserManager:

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

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

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

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

我的 UserModel 是:

class Users(AbstractBaseUser, PermissionsMixin):
    object = UserManager()
    username = models.EmailField(_('UserName'), max_length=75, null=False, blank=False, unique=True)
    first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True)
    last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True)
    tel_number = models.IntegerField(_('TelNumber'), null=True, blank=True)
    address = models.CharField(_('Address'), max_length=250, null=True, blank=True)
    user_image = models.ImageField(_('UserImage'), upload_to='user_avatar', blank=True, null=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)

    def __unicode__(self):
        return self.username

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

    class Meta:
        ordering = ['-id']

    @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

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['tel_number']

现在我想创建超级用户我给出了这个错误:

  

文件" E:\ AB -   Python \ Ads \ lib \ site-packages \ django \ contrib \ auth \ management \ commands \ createsuperuser.py",第149行,句柄       self.UserModel._default_manager.db_manager(数据库).create_superuser(**的user_data)

     

文件" E:\ AB - Python \ Ads \ project \ dijiads \ users \ models.py",第31行,   在create_superuser中       密码=密码

     

文件" E:\ AB - Python \ Ads \ project \ dijiads \ users \ models.py",第19行,   在create_user中       用户名= self.normalize_email(用户名),

     

文件" E:\ AB -   Python \ Ads \ lib \ site-packages \ django \ db \ models \ base.py",第470行,in   的初始化       setattr(self,field.attname,val)AttributeError:无法设置属性

出了什么问题?
如果你有更好的建议写这个模型请告诉我。

1 个答案:

答案 0 :(得分:5)

您可以尝试删除:

@property
def is_superuser(self):
    return self.is_admin
来自Users班级的

is_superuser是用户类继承的permissionsmixin中的字段。