Django excluding specific instances from queryset without using field lookup

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

    you can use "~" with Q Lookup

    for example, the following query will get all tickets from ticket model, except TKT_STATUS field that marked as a "solved" status :

    from django.db.models import Q
    
    queryset_list = ticket.objects.filter(~Q(TKT_STATUS__iexact="solved"))
    

提交回复
热议问题