字段级别权限Django [英] Field Level Permission Django

查看:683
本文介绍了字段级别权限Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我提出了一项要求,要求我实现实地级别的许可,以便寻找最佳的方法.

Today i came up with a requirement where i need to implement field level permission so looking for the best possible way.

class ABC(models.Model):
    field1 = .....
    field2 = .....
    field3 = .....

创建两个组(A和B)并分配权限,两个组都可以添加/编辑/删除 另一个只能添加/编辑.但现在需要一些帮助:-

Create two groups(A and B) and assigned permission that both one can add/edit/delete and the other can only add/edit. But now need some help in this :-

我希望如果第一组用户登录到管理员,他应该能够看到所有三个字段,但是如果第二组用户登录,他们应该只能看到field1.

I want if the first group user logs in in the admin he should be able to see all the three fields but if second group user logs in they should only see field1.

我想在django管理员中使用它,因为我需要在这些之后执行一些操作.我的django版本是1.3

I want this in django admin as i need to perform some manipulations after these.My django version is 1.3

预先感谢

推荐答案

在您的admin.py

class ABCAdmin(admin.ModelAdmin):
    fields = [.....]  # here comes the fields open to all users

    def change_view(self, request, object_id, extra_context=None):  # override default admin change behaviour
        if request.user in gruop2:  # an example 
            self.fields.append('field2')  # add field 2 to your `fields` 
            self.fields.append('field3')  # add field 3 to your `fields`

您可以使用文档查看有什么可用的.上面是从我的用法之一中获取的示例.您可能还需要定义change_viewadd_view.

You can use the docs to see what is available. Above is an example taken from one of my usages. You may also need to define change_view and add_view too.

这篇关于字段级别权限Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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