Django in / not in query Django in / not in query django django

Django in / not in query


table1.objects.exclude(id__in=    table2.objects.filter(your_condition).values_list('id', flat=True))

The exclude function works like the Not operator you where asking for. The attribute flat = True tells to table2 query to return the value_list as a one level list. So... at the end you are obtaining a list of IDs from table2, which you are going to user to define the condition in table1, that will be denied by the exclude function.


with these models:

class table1(models.Model):    field1 = models.CharField(max_length=10)      # a dummy fieldclass table2(models.Model):    key_to_table1 = models.ForeignKey(table1)

you should get what you want using:

table1.objects.exclude(table2=some_param)


table1.objects.extra(where=["table1.id NOT IN (SELECT table2.key_to_table1 FROM table2 WHERE table2.id = some_parm)"])