Django in / not in query

前端 未结 6 1788
不知归路
不知归路 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:34

    [o1 for o1 in table1.objects.all() if o1.id not in [o2.id for o2 in table2.objects.filter(id=some_parm)]]
    

    Or better

    not_in_ids = [obj.id for obj in table2.objects.filter(id=some_parm)]
    selected_objects = [obj for obj in table1.objects.iterator() if obj.id not in not_in_ids]
    

提交回复
热议问题