默认django-admin列表过滤器 [英] Default django-admin list filter

查看:305
本文介绍了默认django-admin列表过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题只是这个线程的扩展[问题] http://stackoverflow.com/questions/851636/default-filter-in-django-admin。

My question is just an extension of this thread [Question]http://stackoverflow.com/questions/851636/default-filter-in-django-admin .

from myproject.myapp.mymodels import fieldC


class Poll(models.Model):

    fieldA = models.CharField(max_length=80, choices=CHOICES.MyCHOICES) 
    fieldB = models.ForeignKey(fieldC)

admin.py

list_display = ('fieldB__fieldc1')

Now my list filter shows four criteria All, A ,B ,C  . 

我想要的是如果超级用户登录,过滤器应显示所有四个条件全部,A ,B,C,如果用户不是超级用户过滤器,则只能显示全部,A,B。

What I want is if the superuser is logged in ,the filter should show all four criteria All,A,B,C and if the user is other than superuser filter should only show All, A, B.

我该如何实现?
这是我实际的一个admin.py

How can i acheive this ? Here is my actual piece of admin.py

def changelist_view(self, request, extra_context=None):

        referer = request.META.get('HTTP_REFERER', '')
        test = referer.split(request.META['PATH_INFO'])
        if test[-1] and not test[-1].startswith('?'):
            if not request.GET.has_key('patient__patient_type__exact'):

                q = request.GET.copy()
                q['patient__patient_type__exact'] = 'Real'
                request.GET = q
                request.META['QUERY_STRING'] = request.GET.urlencode()
                if not request.user.is_superuser:
                    q['patient__patient_type__exact'] = 'Real'
    return super(VisitAdmin, self).changelist_view(request, extra_context)


Thanks in advance 


推荐答案

我认为Django中的新的FilterSpec API o 1.4给你准确的你在这里需要什么查看文档在list_filter上。在1.4中,您现在可以创建子类 django.contrib.admin.SimpleListFilter 的自定义列表过滤器,并赋予您编写自定义查询和查询代码的权力,并且由于请求被传递在你可以使用is_superuser做一个简单的条件。

I think the new FilterSpec API in Django 1.4 gives you exactly what you need here. Check out the docs on list_filter. In 1.4 you can now make custom list filters that subclass django.contrib.admin.SimpleListFilter and give you the power to write custom lookup and queryset code, and since the request is passed in you can do a simple conditional with is_superuser.

if request.user.is_superuser:
    # pass one set of lookups
else:
    # pass a different set

阅读示例代码文件仔细,我认为这一切都会很清楚。

read the example code in the docs carefully and I think it will all be clear.

这篇关于默认django-admin列表过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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