On 6 août, 02:45, nixlists <nixmli...@gmail.com> wrote: > On Fri, Aug 5, 2011 at 5:14 PM, Subhranath Chunder <subhran...@gmail.com> > wrote: > > Your model declarations are very confusing for me,
Indeed. Not using explicit names, and reusing the same name for both a field in the Product model and the foreign keys to Product in other models is a sure way to confuse anyone. (snip more confusing details) > So given a 'contract_id', I am currently finding those like this: > > c = Contract.objects.filter(id = contract_id) # 1/ use Queryset.get instead since you want to retrieve a single contract instance # 2/ use readable names contract = Contract.objects.get(id=contract_id) > cps = c[0].contractproduct_set.filter(contract=contract_id) # 1/ contract.contractproduct_set is already filtered on contract.contract_id, so # no need to filter it again. Please read the doc... # 2/ use readable names contract_products = contract.contractproduct_set.all() > c_dict = dict(cps.values_list('ndc', 'rebate_pct')) > p_dict = dict(cps.values_list('ndc', 'wac__wac')) That's just an unreadable mess, and just plain useless - cf below > for cp in cps: for contract_product in contract_products: > > cset = Claim.objects.filter(contract = contract_id, ndc=cp.ndc_id) # assuming you fix your naming using 'product' intead of 'ndc' for foreign keys claims = contract.claim_set.filter(product=contract_product.product) > ... > rebate = quantity * p_dict[cp.ndc_id] * c_dict[cp.ndc_id] Where does this "quantity" comes from ? Appeared from fresh air ??? Anyway, if you do do have the contract_product instance at hand - which is the case - you can just get the values from it (assuming you also fixed the naming for foreign keys to Pricing): rebate = quantity * contract_product.rebate_pct * contract_product.pricing.wac > Is there a way to do this easier and better? Yes, very obviously. Hints: - read the fine manual, specially the parts about the ORM, querysets and relationships - use appropriate naming > Is it possible to write a model method to access those values from > inside of 'Claim'? If *you* can't answer this question by yourself then you have a serious problem. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.