views:

41

answers:

2

I know I already managed to do this but can't remember how nor I can't find any documentation about this..

How can apply a filter by default on a object list view in the admin ?

I have an app which list quotes and those quotes have a status (ex: accepted, rejected, on hold ..).

I want the filter set on status='accepted' by default that is..

+1  A: 

You can override the queryset

class QuoteAdmin(admin.ModelAdmin):
    def queryset(self, request):
        return super(QuoteAdmin,self).queryset(request).filter(status="accepted")

However by overriding the queryset you won't ever be able to view quotes that do not have status "accepted".

Alternatively, you can link to the following URL, adding the filter to the GET parameters.

/admin/myapp/quote/?status=accepted
Alasdair
A: 

Finally, this is what I was looking for:

def changelist_view(self, request, extra_context=None):
    if not request.GET.has_key('status__exact'):
        q = request.GET.copy()
        q['status__exact'] = '1'
        request.GET = q
        request.META['QUERY_STRING'] = request.GET.urlencode()
    return super(SoumissionAdmin,self).changelist_view(request, extra_context=extra_context)

The other way, with the queryset method in the admin class does not work. In fact it does filter the results, but it leaves the filter functionality broken.

The solution I've found is not perfect either, it's not possible when using it to select the "All/ filter. In my case it's not dramatic and it will be good enough though..

h3