xadmin导航栏过滤怎么做权限设置

1、 找到xadmin的filters.py

E:\DeviceTest\CmAutoTest\common_apps\xadmin\filters.py


2、找到下面的类

class ListFieldFilter(FieldFilter):

3、修改如下

    def get_context(self):
        context = super(ListFieldFilter, self).get_context()
        context['choices'] = list(self.choices())

    # 自定义根据权限过滤导航栏的filter_list里面的值
    if all((self.field.name == "batch", self.title == "批任务id", self.request.user.username != SUPER_USERNAME)):
        new_obj = self.field.related_model.objects.filter(user=self.request.user)
        new_batch_name = [_.batch_name for _ in new_obj]
        new_context = []
        new_context.append(context['choices'][0])
        for i, _ in enumerate(context['choices'][1: -1]):
            if _["display"] in new_batch_name:
                new_context.append(_)
        new_context.append(context['choices'][-1])
        context["choices"] = new_context

    return context</pre>




原创文章,转载请注明出处:http://124.221.219.47/article/123333/