Maximo Arceci
Maximo Arceci

Reputation: 5

Filtering in DJANGO with OR condition

I have a restfull API created with Django and DRF. When I try to filter with OR condition (), it doesn't detect the condition and return all the objects. I want to make it work by a diferent way other than modifying the "get_queryset" method.

I want to find the way to implement some module or backend filter package to implement that filters

Upvotes: 0

Views: 136

Answers (1)

Hafnernuss
Hafnernuss

Reputation: 2827

This can be realized by using Q objects, as stated in the docs.

For example, suppose you have a model User with a CharField type:

User.objects.filter(
    Q(type="admin") | Q(type="moderator")
)

would return all User objects where type is either admin or moderator.

Upvotes: 2

Related Questions