[Answered ]-Why does this Django filter query return empty list?

1👍

Your query looks like Q(tag__slug='car') & Q(tag__slug='awd'), this means that the slug of the same tag should both be Car and Slug, which indeed does not work.

As you found out yourself, you need multiple .filter(…) [Django-doc] calls that will make joins.

You can however also use a trick with an or filter and counting. Indeed, you can work with:

from django.db.models import Count

data = ['car', 'awd']

Vehicle.objects.filter(tags__slug__in=data).alias(ntags=Count('tags')).filter(
    ntags=len(set(data))
)

Leave a comment