477

I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved.

So I basically need to select:

item.creator = owner or item.moderated = False

How would I do this in Django? (preferably with a filter or queryset).

daaawx
  • 3,273
  • 2
  • 17
  • 16
Mez
  • 24,430
  • 14
  • 71
  • 93

8 Answers8

825

There is Q objects that allow to complex lookups. Example:

from django.db.models import Q

Item.objects.filter(Q(creator=owner) | Q(moderated=False))
phoenix
  • 7,988
  • 6
  • 39
  • 45
Alex Koshelev
  • 16,879
  • 2
  • 34
  • 28
  • 11
    how could this be done programmatically? So, for example be able to have `for f in filters: Item.objects.filter(Q(creator=f1) | Q(creator=f2) | ...)` – Alexis Aug 10 '12 at 20:05
  • 16
    @AlexisK Use something like `reduce(lambda q, f: q | Q(creator=f), filters, Q())` to create the big Q object. – Phob Aug 21 '12 at 22:23
  • 34
    @alexis: you could also do `Item.objects.filter(creator__in=creators)`, for example. – Kevin London Dec 09 '14 at 23:11
  • 7
    If you wondering (like me) where `|` being used as OR operator comes from, it's actually the set union operator. It's also used (not here) as bitwise OR: http://stackoverflow.com/questions/5988665/pipe-character-in-python – e100 Mar 26 '15 at 18:06
178

You can use the | operator to combine querysets directly without needing Q objects:

result = Item.objects.filter(item.creator = owner) | Item.objects.filter(item.moderated = False)

(edit - I was initially unsure if this caused an extra query but @spookylukey pointed out that lazy queryset evaluation takes care of that)

Andy Baker
  • 21,158
  • 12
  • 58
  • 71
  • 4
    To find out which queries are executed on a given request, you can use the debug-toolbar Django application. It's made of awesome and win. – Deniz Dogan Apr 11 '09 at 11:45
  • I was testing this from the shell. Is there a way to trace the queries for the above line directly from the shell? – Andy Baker Apr 11 '09 at 16:10
  • 35
    do 'from django.db import connection' and use 'connection.queries'. This requires DEBUG=True. BTW, you should know that [QuerySets are lazy](https://docs.djangoproject.com/en/dev/topics/db/queries/#querysets-are-lazy) and this hits the DB just once. – spookylukey Jun 22 '11 at 17:56
  • 1
    Could exclude be used with negated comparisons? – Neob91 Apr 05 '13 at 21:00
  • 3
    can this result in duplicates in the result queryset? – Charles Haro Feb 11 '17 at 19:51
  • 1
    More specifically query sets tend to hit the DB only when you try to index into them, otherwise you're just building a query. – awiebe Jul 07 '18 at 22:50
  • This is great for my use where I have a loop that produces QuerySets for a bunch of different scenarios, but the last one has an extra case that needs tacked on with an OR. `queries[-1] = queries[-1] | final` – Kevin Mar 19 '21 at 17:56
89

It is worth to note that it's possible to add Q expressions.

For example:

from django.db.models import Q

query = Q(first_name='mark')
query.add(Q(email='mark@test.com'), Q.OR)
query.add(Q(last_name='doe'), Q.AND)

queryset = User.objects.filter(query)

This ends up with a query like :

(first_name = 'mark' or email = 'mark@test.com') and last_name = 'doe'

This way there is no need to deal with or operators, reduce's etc.

marxin
  • 3,692
  • 3
  • 31
  • 44
  • 8
    But it's easier to write `query |= Q(email='mark@test.com')`? – Alex78191 Nov 30 '19 at 02:54
  • 3
    @Alex78191, different folks have different coding style preferences, but besides that, this usage allows the operator (`Q.OR` or `Q.AND`) to be given as an argument to a function that may be required to handle both scenarios. – Kevin Mar 19 '21 at 17:57
40

You want to make filter dynamic then you have to use Lambda like

from django.db.models import Q

brands = ['ABC','DEF' , 'GHI']

queryset = Product.objects.filter(reduce(lambda x, y: x | y, [Q(brand=item) for item in brands]))

reduce(lambda x, y: x | y, [Q(brand=item) for item in brands]) is equivalent to

Q(brand=brands[0]) | Q(brand=brands[1]) | Q(brand=brands[2]) | .....
Abhishek Chauhan
  • 1,984
  • 16
  • 16
30

Similar to older answers, but a bit simpler, without the lambda...

To filter these two conditions using OR:

Item.objects.filter(Q(field_a=123) | Q(field_b__in=(3, 4, 5, ))

To get the same result programmatically:

filter_kwargs = {
    'field_a': 123,
    'field_b__in': (3, 4, 5, ),
}
list_of_Q = [Q(**{key: val}) for key, val in filter_kwargs.items()]
Item.objects.filter(reduce(operator.or_, list_of_Q))

operator is in standard library: import operator
From docstring:

or_(a, b) -- Same as a | b.

For Python3, reduce is not a builtin any more but is still in the standard library: from functools import reduce


P.S.

Don't forget to make sure list_of_Q is not empty - reduce() will choke on empty list, it needs at least one element.

frnhr
  • 12,354
  • 9
  • 63
  • 90
25

Multiple ways to do so.

1. Direct using pipe | operator.

from django.db.models import Q

Items.objects.filter(Q(field1=value) | Q(field2=value))

2. using __or__ method.

Items.objects.filter(Q(field1=value).__or__(field2=value))

3. By changing default operation. (Be careful to reset default behavior)

Q.default = Q.OR # Not recommended (Q.AND is default behaviour)
Items.objects.filter(Q(field1=value, field2=value))
Q.default = Q.AND # Reset after use.

4. By using Q class argument _connector.

logic = Q(field1=value, field2=value, field3=value, _connector=Q.OR)
Item.objects.filter(logic)

Snapshot of Q implementation

class Q(tree.Node):
    """
    Encapsulate filters as objects that can then be combined logically (using
    `&` and `|`).
    """
    # Connection types
    AND = 'AND'
    OR = 'OR'
    default = AND
    conditional = True

    def __init__(self, *args, _connector=None, _negated=False, **kwargs):
        super().__init__(children=[*args, *sorted(kwargs.items())], connector=_connector, negated=_negated)

    def _combine(self, other, conn):
        if not(isinstance(other, Q) or getattr(other, 'conditional', False) is True):
            raise TypeError(other)

        if not self:
            return other.copy() if hasattr(other, 'copy') else copy.copy(other)
        elif isinstance(other, Q) and not other:
            _, args, kwargs = self.deconstruct()
            return type(self)(*args, **kwargs)

        obj = type(self)()
        obj.connector = conn
        obj.add(self, conn)
        obj.add(other, conn)
        return obj

    def __or__(self, other):
        return self._combine(other, self.OR)

    def __and__(self, other):
        return self._combine(other, self.AND)
    .............

Ref. Q implementation

Furkan Siddiqui
  • 1,725
  • 12
  • 19
1

This might be useful https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

Basically it sounds like they act as OR

Dorin Rusu
  • 1,095
  • 2
  • 13
  • 26
-4
Item.objects.filter(field_name__startswith='yourkeyword')
  • 1
    Please add some explanation to your answer such that others can learn from it – Nico Haase Mar 14 '21 at 21:40
  • The question is specifically about building a query in Django to get records with field1 = 'value1' OR field2 == 'value2'. Your answer doesn't answer the question. – Yacc May 12 '22 at 14:22