#8851: Add a default option to list_filter in the admin interface
-------------------------------------+-------------------------------------
     Reporter:  Riskable             |                    Owner:  Harro
  <riskable@…>                       |
         Type:  New feature          |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  admin, list_filter,  |             Triage Stage:  Accepted
  default                            |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Harro):

 Ok, I found a much nicer solution for this whole thing, no need to do
 changes to urls at all.

 Say you have an is_archived flag on a model and you want to default filter
 the list to hide archived items:

 {{{
 class ArchivedListFilter(admin.SimpleListFilter):

     title = _('Archived')
     parameter_name = 'show_archived'

     def lookups(self, request, model_admin):
         return (
             ('all', _('Show all')),
             ('archived', _('Show archived only')),
         )

     def queryset(self, request, queryset):
         show_archived = request.GET.get('show_archived')
         if (show_archived == 'all'):
             return queryset.all()
         elif (show_archived == 'archived'):
             return queryset.filter(is_archived=True)
         return queryset.filter(is_archived=False)

     def choices(self, cl):
         yield {
             'selected': self.value() is None,
             'query_string': cl.get_query_string({},
 [self.parameter_name]),
             'display': _('Hide archived'),  # Changed All to Hide
 archived.
         }
         for lookup, title in self.lookup_choices:
             yield {
                 'selected': self.value() == force_text(lookup),
                 'query_string': cl.get_query_string({
                     self.parameter_name: lookup,
                 }, []),
                 'display': title,
             }
 }}}

 Simply add this filter and by default it will filter and on filter values
 it will either show all items or only the archived items.

 Now it would be nice to be able to change the label in for "All" in the
 choices method without the need to override whole method, so maybe the
 only fix would be to move the label to a propery on SimpleListFilter so
 you can quickly override it.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/8851#comment:40>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/093.25d6560721a085f823d08fe8124354eb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to