Django admin按模型

时间:2016-03-10 11:42:35

标签: django permissions django-admin readonly

当我的用户不是超级用户时,我以只读方式传递TabularInline,并且我的模型管理表的状态与1不同,我通过只读。

但是我无法将相同的测试值应用于has_add_permission。

Admin.py:

class ReadOnlyTabularInline(admin.TabularInline):
    """ class Tabular Inline read only """
    extra = 0
    can_delete = False
    editable_fields = []
    readonly_fields = []
    exclude = []


    def get_readonly_fields(self, request, obj=None):
        """ status filter readony  """

        if request.user.is_superuser or obj.id_status.id_status == 1:
            return super(ReadOnlyTabularInline, self).get_readonly_fields(request, obj)
        else:
            return list(self.readonly_fields) + \
                   [field.name for field in self.model._meta.fields
                    if field.name not in self.editable_fields and
                       field.name not in self.exclude]

我想使用测试值

if request.user.is_superuser or obj.id_status.id_status == 1:

删除按钮添加

def has_add_permissions(self, request):

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

如果当前显示的表单数量超过max_num,则不会显示动态链接。

我使用max_num = 0删除按钮添加

def get_readonly_fields(self, request, obj=None):
    """ status filter readony  """

    if request.user.is_superuser or obj.id_status.id_status == 1:
        return super(ReadOnlyTabularInline, self).get_readonly_fields(request, obj)
    else:
        self.can_delete = False
        self.max_num = 0
        return list(self.readonly_fields) + \
               [field.name for field in self.model._meta.fields
                if field.name not in self.editable_fields and
                   field.name not in self.exclude]