Quantcast
Channel: How to combine multiple querysets in Django? - Stack Overflow
Browsing all 32 articles
Browse latest View live

Answer by Daniel Diaz for How can I combine two or more querysets in a Django...

The best option is to use the Django built-in methods:# Union methodresult_list = page_list.union(article_list, post_list)That will return the union of all the objects in those querysets.If you want to...

View Article


Answer by Vignesh Sk for How can I combine two or more querysets in a Django...

You can use Union:qs = qs1.union(qs2, qs3)But if you want to apply order_by on the foreign models of the combined queryset... then you need to Select them beforehand this way... otherwise it won't...

View Article


Answer by Satyam Faujdar for How can I combine two or more querysets in a...

This will do the work without using any other libraries:result_list = page_list | article_list | post_list

View Article

Answer by Devang Padhiyar for How can I combine two or more querysets in a...

This can be achieved by two ways either.1st way to do thisUse union operator for queryset | to take union of two queryset. If both queryset belongs to same model / single model than it is possible to...

View Article

Answer by Petr Dvořáček for How can I combine two or more querysets in a...

This recursive function concatenates array of querysets into one queryset. def merge_query(ar): if len(ar) ==0: return [ar] while len(ar)>1: tmp=ar[0] | ar[1] ar[0]=tmp ar.pop(1) return ar

View Article


Answer by chidimo for How can I combine two or more querysets in a Django view?

Requirements:Django==2.0.2, django-querysetsequence==0.8In case you want to combine querysets and still come out with a QuerySet, you might want to check out django-queryset-sequence.But one note about...

View Article

Answer by Udi for How can I combine two or more querysets in a Django view?

Related, for mixing querysets from the same model, or for similar fields from a few models, starting with Django 1.11 a QuerySet.union() method is also available:union()union(*other_qs, all=False)New...

View Article

Answer by ray6080 for How can I combine two or more querysets in a Django view?

DATE_FIELD_MAPPING = { Model1: 'date', Model2: 'pubdate',}def my_key_func(obj): return getattr(obj, DATE_FIELD_MAPPING[type(obj)])And then sorted(chain(Model1.objects.all(), Model2.objects.all()),...

View Article


Answer by vutran for How can I combine two or more querysets in a Django view?

In case you want to chain a lot of querysets, try this:from itertools import chainresult = list(chain(*docs))where: docs is a list of querysets

View Article


Answer by bryan for How can I combine two or more querysets in a Django view?

Try this:matches = pages | articles | postsIt retains all the functions of the querysets which is nice if you want to order_by or similar.Please note: this doesn't work on querysets from two different...

View Article

Answer by Jiaaro for How can I combine two or more querysets in a Django view?

Here's an idea... just pull down one full page of results from each of the three and then throw out the 20 least useful ones... this eliminates the large querysets and that way you only sacrifice a...

View Article

Answer by akaihola for How can I combine two or more querysets in a Django view?

Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won't add further cost.from...

View Article

Answer by akaihola for How can I combine two or more querysets in a Django view?

You can use the QuerySetChain class below. When using it with Django's paginator, it should only hit the database with COUNT(*) queries for all querysets and SELECT() queries only for those querysets...

View Article


Answer by Carl Meyer for How can I combine two or more querysets in a Django...

The big downside of your current approach is its inefficiency with large search result sets, as you have to pull down the entire result set from the database each time, even though you only intend to...

View Article

How can I combine two or more querysets in a Django view?

I am trying to build the search for a Django site I am building, and in that search, I am searching in three different models. And to get pagination on the search result list, I would like to use a...

View Article


Answer by Kai - Kazuya Ito for How to combine multiple querysets in Django?

You can use "|"(bitwise or) to combine the querysets of the same model as shown below:# "store/views.py"from .models import Foodfrom django.http import HttpResponsedef test(request): # ↓ Bitwise or...

View Article

Answer by Daniel Diaz for How to combine multiple querysets in Django?

The best option is to use the Django built-in methods:# Union methodresult_list = page_list.union(article_list, post_list)That will return the union of all the objects in those querysets.If you want to...

View Article


Answer by Vignesh Sk for How to combine multiple querysets in Django?

You can use Union:qs = qs1.union(qs2, qs3)But if you want to apply order_by on the foreign models of the combined queryset... then you need to Select them beforehand this way... otherwise it won't...

View Article

Answer by Satyam Faujdar for How to combine multiple querysets in Django?

This will do the work without using any other libraries:result_list = page_list | article_list | post_list

View Article

Answer by Devang Padhiyar for How to combine multiple querysets in Django?

This can be achieved by two ways either.1st way to do thisUse union operator for queryset | to take union of two queryset. If both queryset belongs to same model / single model than it is possible to...

View Article
Browsing all 32 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>