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 Product
s that have a componant that matches for all component_query
s.
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.
Source:stackexchange.com