On Thu, Aug 4, 2011 at 4:10 PM, Javier Guerra Giraldez
<jav...@guerrag.com> wrote:
> On Thu, Aug 4, 2011 at 2:14 PM, nixlists <nixmli...@gmail.com> wrote:
>> I need to rewrite the view so that for each object I need to make a
>> simple calculation based on the data in the fields of that object
>
> you can simply add a method to your model class:
>
> class Claim(models.Model):
>   contract = models.ForeignKey(Contract)
>   ndc = models.ForeignKey(Product)
>   ...
>   quantity = models.IntegerField(blank=True, null=True)
>
>   def mycalc(self)
>       return self.quantity * ndc   # or whatever you need to calculate
>
>
> then, in your template you can simply use:
>
>  {% for c in claims%}
>    {{ c.mycalc }}
>  {% endfor %}

Thanks. How would this work if I need to access values from other
models? Can I define a model method that accesses related data from
other models?

Here's the view code I tried adding the values to QuerySet

cset = Claim.objects.filter(...)

for claim in cset:
    rebate = claim.quantity * price * rebate_pct #price and rebate_pct
are in other models
    claim.rebate = rebate

return list_detail.object_list(
    request,
    queryset = cset,
    paginate_by=paginate_by,
          )

For some reason claim.rebate is not passed to the template, but is
with render_to_response.
return render_to_response('contracts/claim_list.html', {'object_list': cset})

If I have to use render_to_response instead of the generic view, how
would I paginate? I am currently using a django snippet for
pagination:

http://www.tummy.com/Community/Articles/django-pagination/

It works for the generic object_list.

Thanks.

-- 
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.

Reply via email to