Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 200
FSp Avatar asked Nov 11 '14 16:11

FSp


People also ask

How to filter objects in Django based upon the “not equal” condition?

In this section, we will understand how to filter objects in Django based upon the “ Not Equal ” condition. This simply means that we have to filter all the data that does not match the given condition. For this execution, we will use the exclude () method with the QuerySet.

What is the contains filter in Django?

The contains filter in Django returns all the objects that carry case-sensitive strings in the given field. Let’s understand its usage with the help of an example and we will use the Person model in the example. As you can see in the model data, there are multiple records with the same first name but a different last name.

How do I filter for greater than in Python Django?

Python Django filter greater than equal to So, similar to the “ greater than ” filter, we can also use the “ greater than or equal to ” (>=) filter in Django. By using this filter, we can select all the database objects which greater than or equal to a particular value.

How to exclude data from a queryset in Django?

This simply means that we have to filter all the data that does not match the given condition. For this execution, we will use the exclude () method with the QuerySet. The exclude () method in Django returns a new QuerySet with the objects that do not match the given parameter. Let’s understand its usage by executing an example.


1 Answers

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

like image 133
Mithril Avatar answered Sep 23 '22 05:09

Mithril