行权限配置
xadmin中配置且不需要数据库支持,
安装并配置rules
pip安装:
pip install rules
配置settings.py
# settings.py
INSTALLED_APPS = (
# ...
'rules',
)
AUTHENTICATION_BACKENDS = (
'rules.permissions.ObjectPermissionBackend',
'django.contrib.auth.backends.ModelBackend',
)
配置models.py
# models.py
class CompanyUser(models.Model):
users = models.OneToOneField(User, verbose_name='用户名', on_delete=User)
is_admin = models.BooleanField('是否运营人员', default=False)
category_code = models.ManyToManyField(user, verbose_name='可管理', max_length=20, blank=True, default='')
#ForeignKey为单选,ManyToManyField为多选.
def __str__(self):
return str(self.users)
class Meta:
verbose_name = '管理账号'
verbose_name_plural = verbose_name
class user(models.Model):
title = models.CharField(max_length=50, verbose_name="文章标题")
author = models.CharField(max_length=50, null=True, blank=True, verbose_name='文章作者')
#category = models.ForeignKey(Category, verbose_name="所属类别", null=True, blank=True, on_delete=Category)(多选时启用)
content = UEditorField(verbose_name="文章内容", imagePath='static/img/', filePath="static/img/", upload_settings={
"imageMaxSize": 1204000}, default='')
click_num = models.IntegerField(default=0, verbose_name="浏览量", )
image = models.FileField(max_length=200, verbose_name="文章图片", null=True, blank=True, upload_to='static/img', default=None)
add_time = models.DateField(default=datetime.now, verbose_name="添加时间")
def __str__(self):
return self.title
class Meta:
verbose_name = '文章信息'
verbose_name_plural = verbose_name
使用rule
在models的同级目录新增rules.py,配置该app相关的对象权限<