Re: Concept Q: Extra model fields for clean templates sake

2008-09-25 Thread Gerard Petersen

Brian,

Now that I read it ... so obvious. I've come from a long history of procedural 
coding. So I understand OOP but don't take full advantage of it yet ... 
obviously .. :-)

Thanx and Greetz!

Gerard.

Brian Neal wrote:
> On Sep 25, 4: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?
> 
> In addition to what has already been mentioned, it is also handy to
> define convenience functions on your model classes that return
> information like this. So your model could be given a total_price()
> member function that calculates it based upon member data. These
> convenience functions are easily accessed in the template.
> > 

-- 
urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl' }


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



Re: Concept Q: Extra model fields for clean templates sake

2008-09-25 Thread Brian Neal

On Sep 25, 4: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?

In addition to what has already been mentioned, it is also handy to
define convenience functions on your model classes that return
information like this. So your model could be given a total_price()
member function that calculates it based upon member data. These
convenience functions are easily accessed in the template.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Concept Q: Extra model fields for clean templates sake

2008-09-25 Thread Gerard Petersen

Daniel,

On the object dynamicness, I think my brain malfunctioned because somehow I new 
this, but you just helped me remove 3 fields on my product model ... ;-)

As far as placing the calculation in the view or the template (thanks on the 
example btw). An invoice is now generated in html but in the future I want to 
be able to generate them in pdf or ms word as well. So doing the work in the 
view seems like easier transition to those filetypes in the future.


Regards,

Gerard.

Daniel Roseman wrote:
> 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 %}
>> 
>> {{ product.name }}
>> {{ product.description }}
>> {{ product.total }}
>>
> 
> 
> 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.
> > 

-- 
urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl' }


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



Re: Concept Q: Extra model fields for clean templates sake

2008-09-25 Thread Daniel Roseman

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 %}
>         
>             {{ product.name }}
>             {{ product.description }}
>             {{ product.total }}
>


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



Concept Q: Extra model fields for clean templates sake

2008-09-25 Thread Gerard Petersen

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 %}

{{ product.name }}
{{ product.description }}
{{ product.total }}



-- 
urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl' }


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