[Answered ]-How to better format Django filter query for +1000 entries using SQLite3 database? (Expression tree is too large (maximum depth 1000))

1👍

You can filter with:

from django.db.models import Q

component_queries = request.GET['components'].strip().split(',')

Product.objects.filter(
    Q(*[
       Q(component__name__icontains=component_query)
       for component_query in component_queries
    ])
)

The above will however only retrieve Products that have a componant that matches for all component_querys.

If you want to find products that have at least one component for which the string matches, you work with:

from django.db.models import Q

component_queries = request.GET['components'].strip().split(',')

Product.objects.filter(
    Q(*[
       Q(component__name__icontains=component_query)
       for component_query in component_queries
       ],
       _connector=Q.OR
    )
).distinct()

The .distinct() [Django-doc] will prevent retrieving the Product that many times as there are matching components.

Leave a comment