Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
How do you combine two querysets into one queryset(not a list) in django?

I want to simply combine two querysets from different models into one queryset (and not a list as shown in other stackoverflow questions!). For example, here are two querysets:

a = Modelone.objects.all()
b = Modeltwo.objects.filter(number__gte=4)

Then, I want a queryset c that combines both a and b. Thank you, and please leave me any questions you have.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You cannot do that. Imagine the following:

class Modelone(models.model):
 number = models.IntegerField()

class Modeltwo(models.model):
 text = models.TextField()

Imagine you would filter against your joined queryset, how can that work if the rows (or instances) do not have the same fields?

If you would want to join two querysets of the same model, you could do that:

qs1 = modelone.objects.filter(nummber=10)
qs2 = modelone.objects.filter(nummber=20)
joined = qs1 | qs2

(source)

If your two models are similar and have overlapping fields, you could use something like django-polymorphic

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!