【发布时间】:2013-08-12 02:58:56
【问题描述】:
我有一个名为 archive 的 django 模型,它通过外键绑定到 group (django auth)。为了让用户编辑此存档中的某些内容,他们必须通过django-guardian 获得这样做的权限。我可以将存档与组链接,如下面的代码所示,但是,我还需要能够为每个组设置每个对象(存档)的权限。我正在寻求使用post_save 或post_delete 信号来执行此操作(以正确防止孤儿权限)。
换句话说,这是我想要完成的步骤。访问管理页面。创建/编辑Archive 的实例。在该页面上指定哪个用户组将与该特定 Archive. 相关联。保存该实例后,Django 应自动对具有该存档实例的“读取”权限的指定组进行身份验证。
我会将Archive_post_save_handler 存储在什么文件中?模型.py?就目前而言,我无法分配组权限,因为我认为我无法通过我的函数传递对象。我怎样才能做到这一点?作为一个额外的问题,如果我通过 shell 编辑模型,这个钩子是否也适用?
models.py
from django.db import models
from django.contrib.auth.models import Group
from django.db.models.signals import *
from django.dispatch import receiver
class Archive(models.Model):
name = models.CharField(max_length = 30)
nickname = models.CharField(max_length = 30, blank=True)
Group = models.ForeignKey(Group, blank=True, null=True, help_text='A group of users that are allowed access to the archive. This should typically be called the same thing as the archive name.')
class Meta:
permissions = (
('read', 'Read Archive'),
)
def __unicode__(self):
return self.name
@receiver(post_save, sender=Archive)
def Archive_post_save_handler(sender, instance, **kwarks):
group = Group.objects.get(name=instance.Group)
assign_perm('read', group, instance) #what I primarily want to accomplish in this question is on this line
【问题讨论】:
-
我的问题似乎在
assign_perm('read', group, archive)处解决了archive这个短语。我认为我在那里没有正确的变量。
标签: python django python-2.7 django-signals