My models don't really matter in this case, this is a fundamental Python question, I suppose.
Say I have a queryset of items and I want to calculate some things for each one to be displayed in a template.
In my view, I can create a list of objects, and for each object I can set a property on that object for the calculation, then I can display that in the template. OR I can create a list of dictionaries and only get the fields I need to display in each dictionary along with the calculated field. Which is better for performance, and in general practice?
An overly-simplified example for clarity (I know I can call getAge() from the template, what I am really calculated is more complex and for performance I want to do the calculations in the view code):
models.py:
class Person(models.Model):
first_name = ...
last_name = ...
date_of_birth = ...
.
.
.
def getAge(self):
return ... # return the calculated years since date_of_birth
views.py:
def method1_object_property(request):
people = Person.objects.all()
for p in people:
p.age = p.getAge()
return render_to_response('template.htm', {'people': people})
def method2_dictionary(request):
people = Person.objects.all()
data = list()
for p in people:
row = dict()
row['first_name'] = p.first_name
row['last_name'] = p.last_name
row['age'] = p.getAge()
data.append(row)
return render_to_response('template.htm', {'people': data})
template.htm:
<ul>
{% for p in people %}
{{ p.first_name }} {{ p.last_name }} (Age: {{ p.age }})
{% endfor %}
</ul>
Both methods work just fine so far as I can tell, I was just curious what the preferred method would be and why. Are there performance issues assigning new fields dynamically to an existing object in memory using the object dot property method (object.new_field = 'some_detail')?
UPDATE:
Yes, I know in my example I can call getAge() from template, and yes, this is the incorrect naming standard for methods which should be lowercase with underscores. I think my example is too simple and is clouding what I really want to know.
What is the best way to add information to an object that I want displayed in the view that is not a part of the model layer. Say I get a QuerySet of Person
objects and want to calculate how many times they have logged into my website in the last 30, 60 and 90 days. I want to create three "properties" for each Person object on the fly. I can set this in the view with
for p in people:
p.last_30 = Login.objects.filter(person=p, login_date__gt=date.today()-timedelta(days=30))
p.last_60 = Login.objects.filter(person=p, login_date__gt=date.today()-timedelta(days=60))
p.last_90 = Login.objects.filter(person=p, login_date__gt=date.today()-timedelta(days=90))
Then in my template I can display those "properties." I just wanted to make sure I'm not violating some Python standard or cheating the system. Alternately, I could store these other lookups in a dictionary with the object in one key/pair, and the various details in separate ones. This is a bit more work in the view, but I was curious if it is better for performance or compliance of standards to do so?
Sorry if my original question was not clear enough, or my example added confusion.