[Django]-How to merge two different querysets with one common field in to one in django?


You can try this way to capture all the values.

from django.db.models import Subquery, OuterRef, FloatField
from django.db.models.functions import Cast

subquery_qs = proc_order_qs.filter(product_id=OuterRef('product_id')

combined_qs = sales_order_qs.annotate(
    proc_qty = Cast(Subquery(subquery_qs.values('proc_qty')[:1]), output_field=FloatField()),
    proc_price = Cast(Subquery(subquery_qs.values('proc_price')[:1]), output_field=FloatField()))

And then you can get all the values in combined_qs

combined_qs.values('product_id','sales_qty','sales_price','proc_qty', 'proc_price')


you can try to do something like this:

from itertools import chain

def yourview(request):
    Sales_order = ....
    Proc_order = ....
    combined_report = chain(Sales_order,Proc_order)


Leave a comment