我希望模型章节具有两种状态。一个是公开的,另一个仅适用于允许的人群。
因此,我认为我需要对象许可才能实施。我也希望我可以在创建或编辑对象时分配权限
class Chapter(models.Model):
chapter = models.PositiveSmallIntegerField()
title = models.CharField(max_length=200, blank=True)
content = models.TextField()
book = models.ForeignKey('book', on_delete=models.CASCADE, null=True)
AVAILABLE_STATUS = (
('p', 'public'),
('l', 'login user only')
)
status = models.CharField(
max_length = 1,
choices = AVAILABLE_STATUS,
default = 'p',
help_text='Chapter availability',
)
class Meta:
ordering = ['chapter']
unique_together = (("book", "chapter"),)
permissions = (('author', "Can Create ,Edit , Delete"),
('view_private_chapter',"Can View Private Chapter")
)
def save(self, *args, **kwargs):
if self.status=='l':
group = Group.objects.get_or_create(name='private_chap_viewer')
assign_perm('view_private_chapter', group, self)
super().save(*args,**kwargs)
但是我得到错误:当我尝试更改admin中的章节状态时,并非在字符串格式化期间转换了所有参数。我应该如何使用assign_perm?