On Sep 25, 10:33 am, Gerard Petersen <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I'm finding myself creating more and more fields in a model to keep my 
> templates clean. Since it's merely temporary calculated data, and not data to 
> store permanently it feels like the wrong place. An example:
>
> I have a product model (child object of an order model). The product has a 
> 'per_price' and 'amount' field that get filled by the user. I gave the 
> product an extra field called 'total_price' where I store 'amount * 
> per_price' just before my view and my template come together. This way all 
> that's needed to display an invoice is in the objects list.
>
> Calculating this in the template also doesn't seem the way. For MVC's sake I 
> belief this belongs in the view, but then it's to late (read: there's no 
> field on the object) to store temporary stuff.
>
> The Q: How does one relate temporary data to objects in a list, before it's 
> rendered in a template?
>
> Thanx a lot,
>
> Gerard.
>
> Some source snippets if it's not clear:
>
> # ---- Model
> class Product(models.Model):
>     per_price = models.DecimalField('Product price', max_digits=15, 
> decimal_places=2)
>     total_price = models.DecimalField('Calculated', max_digits=15, 
> decimal_places=2, editable=False, null=True)
>     amount = models.IntegerField('#', max_length=10)
>
> # ---- View
> def download_order(request, order_id):
>     for p in product_list:
>         # Calculate product totals
>         p.total = Decimal(p.amount * p.per_price)
>
> # ---- Template
>     {% for product in product_list %}
>         <tr class="{% cycle 'row1' 'row2' %}">
>             <td>{{ product.name }}</td>
>             <td>{{ product.description }}</td>
>             <td>{{ product.total }}</td>
>


Well I'd disagree that you shouldn't do this in the template - this is
exactly the sort of thing that could go there.

However if you're committed to doing it in the view, you should
remember that model instances are just Python objects, so are dynamic.
It's perfectly legal to assign to an attribute on the model which
isn't defined elsewhere. So even if you didn't have a total_price
field, you could still do p.total_price = whatever and then access
that in the template.

Another way of doing it would be to add both the total and the model
to a list:
products_totals = [(p, p.amount * p.per_price) for p in product_list]

and in the template:
{% for product in product_list %}
{{ product.0.name }}
{{ product.0.description }}
{{ product.1 }}

... or you could even make it a list of dictionaries so that the
template is clearer.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to