'Return total value from Subquery - Dja

I have a django query that returns values but I also need to calculate the total quantity in a subquery.

When I have only one value in the subquery table it works fine although if more than one i get an error "More than one row returned by a subquery used as an expression".

Any help would be appreciated.

here is sample of code

def get_machine_components(self):
    return PlantMachineryComponents.objects.filter(machine=self.get_object()).annotate(stock_qty=Sum(Subquery(Stock_DET.objects.filter(stock_hdr__commodity=1275).values('qty'))))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source