1
我的問題只是這個線程的擴展[問題] http://stackoverflow.com/questions/851636/default-filter-in-django-admin。默認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。
我該如何實現這個目標? 這裏是我的實際件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
我很困惑你是否試圖爲用戶和su創建*不同的默認過濾器*,或者爲用戶和su創建不同的過濾器查詢集? –
我對超級用戶和其他用戶都使用相同的過濾器。我只是想在超級用戶登錄時看到他應該看到list_filter中的所有選項,如「真實」,「測試」和「訪客」,他應該能夠過濾該表的行,但如果用戶不是超級用戶,過濾器應該只在list_filter中顯示「Real」和「Guest」選項,他應該只能過濾「Real」和「Guest」選項的行。 –