Search code examples
mysqlsqldjangopython-3.xcontains

How do I form a Django query with an arbitrary number of OR clauses?


I'm using Django 2.0 with Python 3.7. I want to write a query that returns results if the fields contains at least one of the strings in an array, so I want to set up an OR query. I have tried this

class CoopManager(models.Manager):
    ...
    # Meant to look up coops case-insensitively by part of a type
    def contains_type(self, types_arr):
        queryset = Coop.objects.all()
        for type in types_arr:
            queryset = queryset.filter(type__name__icontains=type)
        print(queryset.query)
        return queryset

However, this produces a query that ANDs the clauses together. How do I perform the above but join all the clauses with an OR instead of an AND?

I'm using MySql 5.7 but I'd like to know a db independent solution if one exists.


Solution

  • You can create a Q object that constructs this disjunction:

    from django.db.models import Q
    
    filter = Q(
        *[('type__name__icontains', type) for type in types_arr],
        _connector=Q.OR
    )
    queryset = Coop.objects.filter(filter)

    Here filter is a Q object that is a disjunction of all the type__name__icontains=type filters.