Opened 7 years ago

Closed 7 years ago

#8330 closed (duplicate)

Cannot add new filterspecs without modifining

Reported by: elwaywitvac <elwaywitvac@…> Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: Filters
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Adding your own filter specs works easily by simply using this code within an applications

from django.contrib.admin.filterspecs import FilterSpec

class MyFilterSpec(FilterSpec):


However, since the last default FilterSpec is always evaluated as True, and new FilterSpec's never get tested. It doesn't seem right to require a library file be modified in order to add a a feature to an application (for example, django-tagging which is what I was messing with when I found this.)

Since I don't know exactly what channels I should be going through to submit this as a patch, I'll just put the solution I found here. It's easy to fix if you add the following the FilterSpec.

class FilterSpec(object):
    filter_specs = []
    default = None  #added
    def create(cls, f, request, params, model, model_admin):
        for test, factory in cls.filter_specs:
            if test(f):
                return factory(f, request, params, model, model_admin)
        ### added after this point
        if callable(default):
            return default(f, request, params, model, model_admin)
    create = classmethod(create)

    # new method
    def set_default(cls, factory):
        if callable(factory):
                cls.default = factory
    set_default = classmethod(set_default)

Then, just replace the last line (which registers AllValuesFilterSpec) with:


Attachments (1) (7.8 KB) - added by elwaywitvac <elwaywitvac@…> 7 years ago.
New version of

Download all attachments as: .zip

Change History (2)

Changed 7 years ago by elwaywitvac <elwaywitvac@…>

New version of

comment:1 Changed 7 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Closing as a dupe of #5833.

Note: See TracTickets for help on using tickets.
Back to Top