Re: Efficiency of getting attribute values in templates

2009-07-07 Thread Nikola Pavlović

On Fri, Jul 03, 2009 at 09:04:37AM -0700, Rajesh D wrote:
> 
> On Jul 3, 9:17 am, Nikola Pavlović  wrote:
> > Hello people,
> >
> > I'm writing my first Django application and would like to know what is
> > considered more efficient: getting values of models' attributes in views
> > or directly in templates?
> >
> > More precicely, say we have a Person model with name and id attributes,
> > and these need to be shown on a page.
> >
> > Is it better for a view to "prepare" a context like this:
> >
> > # views.py
> > # ...
> >
> >     persons = []
> >
> >     for person in Person.objects.all():
> >         persons.append({'id': person.id, 'name': person.name})
> >
> >     return render_to_response('some_template.html', persons)
> >
> > # some_template.html
> >
> > {% for p in persons %}
> >     Id: {{ p.id }}; Name: {{ p.name }}
> > {% endfor %}
> >
> > or just pass a QuerySet in a context like this:
> > # views.py
> > # ...
> >
> >     persons = Person.objects.all()
> >     return render_to_response('some_template.html', persons)
> >
> > and then let the template access attribute values directly?
> >
> > Does it make a difference in terms of performance?
> 
> The second method avoids an extra loop and also keeps your code leaner
> (less code == lesser chances of bugs). It's also the more commonly
> used method. And if you are displaying just a handful of persons per
> page, the performance differences should be negligible either way. 

Great, that was just what I wanted to know -- a 'Django best practices'
kind of question.

After a couple of hours of playing with this the other day, I came to
the same ("obvious") conclusion (much simpler code), but I was just 
concerned if there was some significant overhead to accesing attributes 
in the rendering phase.


Thanks!


-- 
Two can Live as Cheaply as One for Half as Long.
-- Howard Kandel


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



Re: Efficiency of getting attribute values in templates

2009-07-05 Thread Rajesh D



On Jul 4, 9:28 pm, TechnicalBard  wrote:
> But the admin app does - it defines the labels from the attribute
> names and just puts them all in a table or list.
>
> How does one do that?

Actually, you tell it which fields you want to display through the
list_display attribute on your Admin class.

  Analogous to iterating through a list of
> objects, is it possible to iterate through the attributes of a single
> object?

The _meta object of a model instance has a method that can give you
all the field names:

your_model_instance._meta.get_all_field_names()

There are several other methods and properties like this available on
the _meta[1] object (for example, get_field(), .fields,..) Once you
learn what they do, you will be able to deduce the field names you
need and then call getattr(your_model_instance, field_name) on them.
You will probably also want to distinguish between local fields and
related fields such as foreign keys and m2m fields. You can't do much
of this in the template. You will have to do all this introspection in
your view and then pass on suitable context variables to your template
which can be fairly generic.

Martin Alchin's book Pro Django[2] is a must-read if you want to dive
into this kinda of stuff.

[1] 
http://code.djangoproject.com/browser/django/trunk/django/db/models/options.py
[2] http://www.amazon.com/Pro-Django-Experts-Voice-Development/dp/1430210478

-RD


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



Re: Efficiency of getting attribute values in templates

2009-07-04 Thread TechnicalBard

But the admin app does - it defines the labels from the attribute
names and just puts them all in a table or list.

How does one do that?  Analogous to iterating through a list of
objects, is it possible to iterate through the attributes of a single
object?

On Jul 4, 5:32 pm, Rajesh D  wrote:
> On Jul 4, 6:30 pm, TechnicalBard  wrote:
>
> > Is there a way to pass an object to the template and have the template
> > handle all of the attributes without hardcoding them into the
> > template?
>
> Depends on what you mean by "handle". The template won't by itself
> know the markup with which you want to wrap your object's various
> attributes.
>
> -RD
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Efficiency of getting attribute values in templates

2009-07-04 Thread Rajesh D



On Jul 4, 6:30 pm, TechnicalBard  wrote:
> Is there a way to pass an object to the template and have the template
> handle all of the attributes without hardcoding them into the
> template?

Depends on what you mean by "handle". The template won't by itself
know the markup with which you want to wrap your object's various
attributes.

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



Re: Efficiency of getting attribute values in templates

2009-07-04 Thread TechnicalBard

Is there a way to pass an object to the template and have the template
handle all of the attributes without hardcoding them into the
template?

On Jul 3, 10:04 am, Rajesh D  wrote:
> On Jul 3, 9:17 am, Nikola Pavlović  wrote:
>
>
>
>
>
> > Hello people,
>
> > I'm writing my first Django application and would like to know what is
> > considered more efficient: getting values of models' attributes in views
> > or directly in templates?
>
> > More precicely, say we have a Person model with name and id attributes,
> > and these need to be shown on a page.
>
> > Is it better for a view to "prepare" a context like this:
>
> > # views.py
> > # ...
>
> >     persons = []
>
> >     for person in Person.objects.all():
> >         persons.append({'id': person.id, 'name': person.name})
>
> >     return render_to_response('some_template.html', persons)
>
> > # some_template.html
>
> > {% for p in persons %}
> >     Id: {{ p.id }}; Name: {{ p.name }}
> > {% endfor %}
>
> > or just pass a QuerySet in a context like this:
> > # views.py
> > # ...
>
> >     persons = Person.objects.all()
> >     return render_to_response('some_template.html', persons)
>
> > and then let the template access attribute values directly?
>
> > Does it make a difference in terms of performance?
>
> The second method avoids an extra loop and also keeps your code leaner
> (less code == lesser chances of bugs). It's also the more commonly
> used method. And if you are displaying just a handful of persons per
> page, the performance differences should be negligible either way. If
> you have a lot of persons in that query set, you will want to use some
> kind of pagination anyway.
>
> -RD
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Efficiency of getting attribute values in templates

2009-07-03 Thread Rajesh D



On Jul 3, 9:17 am, Nikola Pavlović  wrote:
> Hello people,
>
> I'm writing my first Django application and would like to know what is
> considered more efficient: getting values of models' attributes in views
> or directly in templates?
>
> More precicely, say we have a Person model with name and id attributes,
> and these need to be shown on a page.
>
> Is it better for a view to "prepare" a context like this:
>
> # views.py
> # ...
>
>     persons = []
>
>     for person in Person.objects.all():
>         persons.append({'id': person.id, 'name': person.name})
>
>     return render_to_response('some_template.html', persons)
>
> # some_template.html
>
> {% for p in persons %}
>     Id: {{ p.id }}; Name: {{ p.name }}
> {% endfor %}
>
> or just pass a QuerySet in a context like this:
> # views.py
> # ...
>
>     persons = Person.objects.all()
>     return render_to_response('some_template.html', persons)
>
> and then let the template access attribute values directly?
>
> Does it make a difference in terms of performance?

The second method avoids an extra loop and also keeps your code leaner
(less code == lesser chances of bugs). It's also the more commonly
used method. And if you are displaying just a handful of persons per
page, the performance differences should be negligible either way. If
you have a lot of persons in that query set, you will want to use some
kind of pagination anyway.

-RD


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



Efficiency of getting attribute values in templates

2009-07-03 Thread Nikola Pavlović

Hello people,

I'm writing my first Django application and would like to know what is
considered more efficient: getting values of models' attributes in views
or directly in templates?

More precicely, say we have a Person model with name and id attributes,
and these need to be shown on a page. 

Is it better for a view to "prepare" a context like this:

# views.py
# ...

persons = []

for person in Person.objects.all():
persons.append({'id': person.id, 'name': person.name})

return render_to_response('some_template.html', persons)

# some_template.html

{% for p in persons %}
Id: {{ p.id }}; Name: {{ p.name }}
{% endfor %}


or just pass a QuerySet in a context like this:
# views.py
# ...

persons = Person.objects.all()
return render_to_response('some_template.html', persons)


and then let the template access attribute values directly?

Does it make a difference in terms of performance?



-- 
Alex Haley was adopted!


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