[Answered ]-Return all model results, but filter out the ManyToManyFields

1👍

You can use Prefetch() objects [Django docs] and pass the queryset keyword argument to do this:

from django.db.models import Prefetch

foo_queryset = Foo.objects.prefetch_related(
    Prefetch(
        'bar_set',
        queryset=Bar.objects.filter(is_public=True),
        to_attr='public_bars'
    )
)

Now you can access the related Bar instances which are public by using:

for foo in foo_queryset:
    print(foo.public_bars)

Leave a comment