Categories
Python Answers

How to combine two or more querysets in a Python Django view?

Spread the love

To combine two or more querysets in a Python Django view, we can use the itertools chain method.

For instance, we write

from itertools import chain
result_list = list(chain(page_list, article_list, post_list))

to call chain with the page_list, article_list, and post_list querysets.

Then we convert the combined querysets into a list with list.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *