#8851: Add a default option to list_filter in the admin interface
-------------------------------------+-------------------------------------
     Reporter:  Riskable             |                    Owner:  Harro
  <riskable@…>                       |
         Type:  New feature          |                   Status:  new
    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 Aymeric Augustin):

 Furthermore, while `SimpleListFilter` is a public API, `FieldListFilter`
 isn't. The docs note:

 > The `FieldListFilter` API is considered internal and might be changed.

 There's no way to make a good list filter for handling soft-deletes with
 `SimpleListFilter` because it defaults to showing everything, which is the
 main behavior that needs changing.

 Here's how I did it with `FieldListFilter`:

 {{{
 class DeactivableListFilter(admin.ListFilter):

     title = _("active")
     parameter_name = 'active'

     def __init__(self, request, params, model, model_admin):
         super().__init__(request, params, model, model_admin)
         if self.parameter_name in params:
             value = params.pop(self.parameter_name)
             self.used_parameters[self.parameter_name] = value

     def show_all(self):
         return self.used_parameters.get(self.parameter_name) == '__all__'

     def has_output(self):
         return True

     def choices(self, changelist):
         return [
             {
                 'selected': self.show_all(),
                 'query_string':
 changelist.get_query_string({self.parameter_name: '__all__'}),
                 'display': "Tout",
             },
             {
                 'selected': not self.show_all(),
                 'query_string':
 changelist.get_query_string(remove=[self.parameter_name]),
                 'display': "Actif",
             },
         ]

     def queryset(self, request, queryset):
         if not self.show_all():
             return queryset.filter(active=True)

     def expected_parameters(self):
         return [self.parameter_name]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/8851#comment:44>
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.1a52573ae41e2029546d02b88e914c93%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to