Django in / not in query

前端 未结 6 1776
不知归路
不知归路 2020-12-02 14:56

I\'m trying to figure out how to write a \'not in\' style query in django. For example, the query structure I\'m thinking of would look like this.

select tab         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 15:48

    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.

提交回复
热议问题