Django: how to exclude instances from the relationship of another model?

advertisements

This question already has an answer here:

  • In a Django QuerySet, how to filter for “not exists” in a many-to-one relationship 4 answers

I have two models:

Model A:
    field1

Model B:
    a_list = ManyToManyField(A)

Now, I want to find A not in the list of B's a_list, like:

A.objects.exclude(self__in=b.a_list)

How can I do this?


Something along these lines should work:

A.objects.all().exclude(pk__in=b.a_list)