如何获取Django中两个查询集的区别? [英] How to get the difference of two querysets in Django?
本文介绍了如何获取Django中两个查询集的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须查询集。所有列表和订阅列表
I have to querysets. alllists and subscriptionlists
alllists = List.objects.filter(datamode = 'A')
subscriptionlists = Membership.objects.filter(member__id=memberid, datamode='A')
我需要一个名为unsubscriptionlist的查询集拥有所有列表中的所有记录,但订阅列表中的记录除外。如何实现这一目标?
I need a queryset called unsubscriptionlist, which possess all records in alllists except the records in subscription lists. How to achieve this?
推荐答案
自Django 1.11开始,QuerySets就有了 difference()
方法以及其他新方法:
Since Django 1.11, QuerySets have a difference()
method amongst other new methods:
# Capture elements that are in qs_all but not in qs_part
qs_diff = qs_all.difference(qs_part)
另请参阅: https:// stackoverflow .com / a / 45651267/5497962
这篇关于如何获取Django中两个查询集的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文