tags:

views:

1608

answers:

5

how can i change the default filter choice from 'ALL'.I have a field named as 'status' which has three values :- 'activate','pending','rejected'.when i use list_filter in django_admin ,the filter is by default set to 'All' but i want to set it to pending by default.

+3  A: 
class MyModelAdmin(admin.ModelAdmin):   

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

        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)
ha22109
Extremely cool. I needed exactly this just 13 hours after you had submitted the answer. Thanks Paolo, thanks Stack Overflow, thanks Google, thanks Django.
akaihola
i thougth i have answer the question.But the only problem was that my answer was not formatted.cool.it was like this
ha22109
Why did you roll it back? Your code is missing the 'c' of class, and is hard to read without the formatting.
Dominic Rodger
know correct.it is formatted
ha22109
This solution has the drawback that although the "All" choice is still displayed in the UI, selecting it still applies the default filtering.
akaihola
i have the same question, but i can understand the replay...sorry im new with Django... but maybe this will workhttp://blog.dougalmatthews.com/2008/10/filter-the-django-modeladmin-set/
Asinox
+1  A: 

Note that if instead of pre-selecting a filter value you want to always pre-filter the data before showing it in the admin, you should override the ModelAdmin.queryset() method instead.

akaihola
This is a pretty clean and quick solution although it may still cause problems. When the filtering options are enabled in the admin the user may get seemingly incorrect results. If the overriden queryset contains an .exclude() clause then records caught by that will never be listed but the admin filtering options to explicitly show them will still be offered by the admin UI.
TomA
A: 

I know that is not the best solution, but i changed the index.html in the admin template, line 25 and 37 like this:

25: <th scope="row"><a href="{{ model.admin_url }}{% ifequal model.name "yourmodelname" %}?yourflag_flag__exact=1{% endifequal %}">{{ model.name }}</a></th>

37: <td><a href="{{ model.admin_url }}{% ifequal model.name "yourmodelname" %}?yourflag__exact=1{% endifequal %}" class="changelink">{% trans 'Change' %}</a></td>

mdgart
A: 

Hi all

I have a followup question: How can I get a QuerySet instance from the ModelAdmin with filters set as defined by the query string. For instance, ?start_date_gte=2009-11-06

The queryset() method does not seem to use these filters.

Sam
A: 

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)
iridescent