How would I do a not in
filter in Django when I have related fields?
class Publisher(Model):
pass
class Author(Model):
name = models.CharField(...)
publisher = models.ForeignKey(Publisher)
I can write:
Publisher.objects.filter(author__name__in=XXX)
this does not work:
Publisher.objects.filter(author__name__not_in=XXX)
And while this Django equivalent of SQL not in claims you can use exclude
, this is not correct:
Publisher.objects.exclude(author__name__in=XXX)
XXX
is a list of names.
To clarify what I want to get: I would like to find all publishers that have an author NOT in that list. Note: these publishers may also have an author IN that list.
Let's say I have two publishers A, B and the following Authors for the publishers:
A: Alex, Bob, Greg
B: Alex, Greg
xxx is ['Alex', 'Greg']
I would like to find any Publisher that has an Author not in that list. In this case A should be returned since A has author Bob which is not in that list.