带后保存信号的Django Assign-perm [英] Django Assign-perm w/ Post-Save Signal

查看:60
本文介绍了带后保存信号的Django Assign-perm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 archive 的Django模型,该模型通过外键绑定到 group (django auth).为了使用户能够编辑此归档文件中的内容,他们必须具有通过 django-guardian 进行编辑的权限.我可以将归档文件与一个组链接,如下面的代码所示,但是,我还需要能够为每个组设置按对象(归档)权限.我正在尝试使用 post_save post_delete 信号执行此操作(以正确防止孤立权限).

I have a django model named archive that is tied to a group (django auth) by a foreign key. In order for a user to edit something in this archive, they must have permission to do so through django-guardian. I can link an archive with a group as shown in my code below, however, I need to be able to set per-object (archive)-permissions with each group as well. I am seeking to do this using a post_save or post_delete signal (to properly prevent orphan permissions).

换句话说,这是我要完成的步骤.访问管理页面.创建/编辑 Archive 的实例.在该页面上指定将与该特定 Archive相关联的用户组.保存该实例后,Django应使用该Archives实例的读取"权限自动对指定的组进行身份验证.

In other words, here are the steps I want to accomplish. Visit Admin page. Create/Edit an instance of Archive. Specify on that page what user-group will be associated with that particular Archive. Upon saving that instance, Django should automatically authenticate the specified group with 'read' permissions for that instance of Archive.

我将在其中存储 Archive_post_save_handler 的哪个文件?models.py?就目前而言,我无法分配组权限,因为我认为无法通过函数传递对象.我该怎么做?作为一个额外的问题,如果我通过外壳编辑模型,该钩子也适用吗?

What file would I store Archive_post_save_handler in? models.py? As it stands, I can't assign group permissions because I think I am having trouble passing objects through my functions. How can I accomplish this? As a bonus question, will this hook also apply if I edited the model through a shell?

models.py

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

推荐答案

我认为您在此处粘贴的代码将导致递归调用 Archive_post_save_handler ,因为最后一行代码也会触发另一个 post_save 信号.您应该从kwargs中获取 created 参数,以测试实例是否是首次创建.

I think the code you pasted here will cause a recursive calling Archive_post_save_handler, because the last line of code will also trigger another post_save signal. You should get the created argument from the kwargs to test if the instance is created for the first time.

这篇关于带后保存信号的Django Assign-perm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆