merge django queryset objects

How to join querysets in django

lets learn how to merge two or more models data into one object with the help of itertools

In views.py just write

from itertools import chaindef myfunc(request):
data1 = model1.objects.all()
data2 = model2.objects.all()
combined_data = list(chain(data1,data2))
print(combined_data)
return httpresponse("success")

and then you can iterate over the result object . you can use this method to combine more than two objects also .

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response