Can we do arithmetic using Django Subqueries?

Adam Starrh

I am wondering if Django's ORM allows us to do aggregate operations on subqueires, and then do arithmetic with the resulting values.

What would be the proper way to go about something like this:

record = PackingRecord.objects.filter(product=OuterRef('pk'))
packed = FifoLink.objects.filter(packing_record__product=OuterRef('pk'))

output = obj_set.annotate(
    in_stock=(Subquery(record.aggregate(Sum('qty'))) - Subquery(packed.aggregate(Sum('sale__qty'))))
).values('id', 'name', 'in_stock')
ivissani

You certainly can but as far as I have dig, you cannot use aggregate(). When trying to use aggregate() in a Subquery django complains about trying to execute a query that has OuterRefs. The way I do this (I really don't know if this is THE way - according to the docs it is -) is by using annotate(). In a case like the one you have in your example I'd do something like the following:

records_total = (PackingRecord.objects.filter(product=OuterRef('pk'))
    .values('product') # Group by product
    .annotate(total=Sum('qty')) # Sum qty for 'each' product
    .values('total')
)
packed_total = (FifoLink.objects.filter(packing_record__product=OuterRef('pk'))
    .values('packing_record__product') # Group by packing_record__product
    .annotate(total=Sum('sale__qty')) # Sum sale__qty for 'each' product
    .values('total')
)
output = obj_set.annotate(
    r_tot=Subquery(record_total[:1]),
    p_tot=Subquery(packed_total[:1])
).annotate(
    in_stock=F('r_tot')-F('p_tot')
) # Whatever you need

I did not run the example, so it may need some adjustments here and there.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can we perform an arithmetic operation on a register using GDB?

Django ValueError: Can't do subqueries with queries on different DBs

Can we do a Sum on CharField in Django ORM?

Can we perform arithmetic operation in positional formatting?

Can regex do basic arithmetic?

How do I use Django subqueries?

How to know where are all places we can use subqueries in mysql?

Can we reduce redundancy between two subqueries in an update statement?

Why do we use underscore "_" in this python function using django?

Can we do this in perl?

How can we do switch case using string

Multithreading: How can we do Time Slicing using Java?

Can we do web automation without using selenium/ QTP etc?

Zooming and Panning in JS/JQuery like we can do it using SVG

Can we do VOIP push notification using Twilio iOS SDK?

Without using aggregate function can we do pivot in sql

Can we use arithmetic operators (+, -. *, or /) on date functions in mysql?

Can we use double to store monetary fields and use BigDecimal for arithmetic

Can we use conventional pointer arithmetic with std::array?

need to do arithmetic operations in django templates tags

Can bash do floating-point arithmetic without using an external command?

Why do we need to extend when using generics in Java if we can just use the original type?

Why do we need ViewChild() when we can communicate with Child Controller using @Output decorator

Why do we implement Cloneable even if we can go for deep cloning using the following snippet

Can Lua do an arithmetic right shift?

Can't do arithmetic operation in bash

How to do a arithmetic sequence with roots using a loop

Do we have some package or module in django through which we can see real-time changes of our html files in django as we edit the code?

After performing arithmetic operations within a django model, how can I update the value of an integer in my model using the new value I obtained?