Django excluding specific instances from queryset without using field lookup

后端 未结 4 942
南笙
南笙 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:30

    The Given answer is perfect and try this which works fine for me

    step 1)

     from django.db.models import Q
    

    step 2)

     MyModel.objects.filter(~Q(id__in=[o.id for o in ]))
    

提交回复
热议问题