nick_rinaldi
nick_rinaldi

Reputation: 707

Django queryset filtering not providing expected behavior

I have an app taking inputs from a user on the front end.

The functionality I'm trying to implement should display all titles on the front end that have players <=0, or display NULL values in the database.

 player_filter = 'lte'
 if playerinput_high == '0':
        player_kwargs = {
                'player_points__isnull': True,
                'player_points__{}'.format(reviewsign_high): float(playerinput_high)
            }

        stats = stats.filter(**player_kwargs)

However, I don't get any results back here. If I remove one parameter, i.e. 'player_reviews__isnull': True, I do get all titles with reviews less than or equal to 0.

Vice versa, if I remove 'player_reviews__{}'.format(playersign_high): float(playerinput_high), I get all stats with NULL stats. But them together, displays 0 results. Any ideas on a solution here? Thanks!

Upvotes: 0

Views: 42

Answers (1)

Jimmy Pells
Jimmy Pells

Reputation: 714

Multiple filters in Django's ORM default to 'AND' when generating SQL. You should look into using the Q object to specify an 'OR' query. https://docs.djangoproject.com/en/3.2/topics/db/queries/#complex-lookups-with-q-objects

from django.db.models import Q
titles = titles.filter(Q(**review_kwargs, _connector=Q.OR))

Upvotes: 1

Related Questions