我如何找到两个Django querysets的联合? [英] How can I find the union of two Django querysets?
本文介绍了我如何找到两个Django querysets的联合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法获取查询集,或只是一个对象列表,这是每个管理员方法返回的查询的联合?
解决方案
这个工作和看起来有点清洁:
records = query1 | query2
如果不想重复,那么您需要附加 .distinct()
:
records =(query1 | query2).distinct()
I’ve got a Django model with two custom manager methods. Each returns a different subset of the model’s objects, based on a different property of the object.
Is there any way to get a queryset, or just a list of objects, that’s the union of the querysets returned by each manager method?
解决方案
This works and looks a bit cleaner:
records = query1 | query2
If you don't want duplicates, then you will need to append .distinct()
:
records = (query1 | query2).distinct()
这篇关于我如何找到两个Django querysets的联合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文