How to get the difference of two querysets in Django?

前端 未结 4 1044
一个人的身影
一个人的身影 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:27

    How about:

    subscriptionlists = Membership.objects.filter(member__id=memberid, datamode='A')
    unsubscriptionlists = Membership.objects.exclude(member__id=memberid, datamode='A')
    

    The unsubscriptionlists should be the inverse of subscription lists.

    Brian's answer will work as well, though set() will most likely evaluate the query and will take a performance hit in evaluating both sets into memory. This method will keep the lazy initialization until you need the data.

提交回复
热议问题