Search code examples
pythondjangofilterdjango-admindjango-admin-filters

django admin list_filter "or" condition


sorry if this question has been answered before, but I did a lot of googling without success.

I know how to create custom list_filters in admin views (e.g. subclassing SimpleFilter).

What I really would like, is a way (on the admin list view) to "check" different filters that combines them in a OR formula.

As an example, suppose you have:

# models.py
class Foo(models.Model):
    foobar = ...
    foofie = ...
...

# admin.py
class FooAdmin(admin.ModelAdmin):
    list_filter = ( "foobar", "foofie" )
...

In the admin list view generated by FooAdmin I can choose to filter records either by foobar or by foofie. Is there a way to filter them by the formula: foobar = X OR foofie = Y, where X and Y are two values that foobar and foofie can assume?

Is it even possible?

I know not everything is possible in the django admin views, but it seems a very common request, I wonder if I missed to understand or read something.

Also third party apps allowing it are welcome. Thanks :)


Solution

  • I found a third party app just now, it is django-advanced-filters which may fit you requirement .

    It has:

    The OR field

    OR is an additional field that is added to every rule's available fields.

    It allows constructing queries with OR statements. You can use it by creating an "empty" rule with this field "between" a set of 1 or more rules.

    I have run a test, add a OR field would work. This is the screenshot: enter image description here