How to get the difference of two querysets in Django?

前端 未结 4 1008
一个人的身影
一个人的身影 2020-12-29 04:41

I have to querysets. alllists and subscriptionlists

alllists = List.objects.filter(datamode = \'A\')
subscriptionlists = Membership.objects.filter(member__id         


        
4条回答
  •  被撕碎了的回忆
    2020-12-29 05:35

    Well I see two options here.

    1. Filter things manually (quite ugly)

    diff = []
    for all in alllists:
        found = False
        for sub in subscriptionlists:
            if sub.id == all.id:
                found = True 
                break
        if not found:
            diff.append(all)
    

    2. Just make another query

    diff = List.objects.filter(datamode = 'A').exclude(member__id=memberid, datamode='A')
    

提交回复
热议问题