Please see simplified django model representation on 
pastebin<http://pastebin.com/c90WEyPv> 

In this scheme of things, there are several SiteOffice objects that are 
governed by a single HeadOffice object (I haven't included either in the 
pastebin code). The head_office instance generates a 
purchase_orderindependently. A 
site_office generates a material_requirement and now it is up to the 
head_office to see whether such a purchase_order exists which has a usable 
PurchaseOrder.order_quantity left yet to be delivered that can be used to 
fulfill this material_requirement. If such a purchase_order exists, the 
head_office generates a delivery_order to fulfill the material_requirement.

Now this means that there exist several PurchaseOrder objects that *may or 
may not have any corresponding* DeliveryOrder objects. I am trying to query 
PurchaseOrder to find instances that can fulfill a particular 
material_requirement.quantity.

How can I use the Django ORM to find PurchaseOrder instances where 
PurchaseOrder.order_quantity 
>= Sum(DeliveryOrder.quantity) + MaterialRequirement.quantity for all 
DeliveryOrder instances that refer back to the PurchaseOrder. I am getting 
stuck as only those PurchaseOrder instances are getting returned for which 
at least one matching DeliveryOrder instance exists. There exist other 
PurchaseOrder instances for which no corresponding DeliveryOrder exists, 
and whose PurchaseOrder.order_quantity >= material_requirement.quantity and 
I would like those to be returned as well.

I couldn't figure out a way to do this through the ORM, so I tried to use 
raw sql. But there too I'm getting stuck. Please see 
http://stackoverflow.com/questions/15610238 for what I have been up to.

Thanks.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to