[Answered ]-Django filter for ManyToMany items which exist only in a specified list

1👍

You can filter out such items by looking for Categorys that have at least one item that is not keywords_1/keywords_2, or have no items, so:

from django.db.models import Exists, OuterRef, Q

Category.objects.filter(
    Exists(
        CategoryGroup.objects.filter(
            ~Q(label__in=['keywords1', 'keywords2']),
            category_group=OuterRef('pk'),
        )
    )
)

Leave a comment