Django excluding specific instances from queryset without using field lookup

后端 未结 4 933
南笙
南笙 2020-12-23 14:13

I sometimes have the need to make sure some instances are excluded from a queryset.
This is the way I do it usually:

unwanted_instance = MyModel.object         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 14:44

    You can put your unwanted items in a list , and a fetch all items except those in the list like so:

    MyModel.objects.exclude(id__in=[id1,id2,id3 ])
    

提交回复
热议问题