Default filter in Django admin

前端 未结 15 1889
有刺的猬
有刺的猬 2020-11-27 10:49

How can I change the default filter choice from \'ALL\'? I have a field named as status which has three values: activate, pending and

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 11:00

    Took ha22109's answer above and modified to allow the selection of "All" by comparing HTTP_REFERER and PATH_INFO.

    class MyModelAdmin(admin.ModelAdmin):
    
        def changelist_view(self, request, extra_context=None):
    
            test = request.META['HTTP_REFERER'].split(request.META['PATH_INFO'])
    
            if test[-1] and not test[-1].startswith('?'):
                if not request.GET.has_key('decommissioned__exact'):
    
                    q = request.GET.copy()
                    q['decommissioned__exact'] = 'N'
                    request.GET = q
                    request.META['QUERY_STRING'] = request.GET.urlencode()
            return super(MyModelAdmin,self).changelist_view(request, extra_context=extra_context)
    

提交回复
热议问题