Is there a way to obtain a QuerySet in Django so that the remaining objects satisfy at least one of a number of constrains (i.e. an OR statement)?
remaining = Fruits.objects.all()
fruit_type = ['apple', 'orange'] # input from user
for fruit in fruit_type:
remaining = remaining.filter(FruitType__icontains=fruit)
You could use Q object to perform complex queryset
from django.db.models import Q for fruit in fruit_type: query |= Q(FruitType__icontains=fruit) # Query the model remaining = remaining.filter(query)
Refer to django docs https://docs.djangoproject.com/en/1.10/topics/db/queries/#complex-lookups-with-q for more information.