Re: assigning variables in templates

2008-10-20 Thread Chris Emerson

Hi,

On Wed, Oct 15, 2008 at 01:46:38PM -0700, bmt wrote:
> my problem is that when this list is empty i want to branch in the
> template, and display some sort of message, instead of the usual 'for'
> loop which would display the returned list. since i can't assign the
> returned list to a variable i have to check the method's return value
> first with an 'if' tag than run the query again for the 'for' loop.
> this doubles the load on the db what i don't like, not to mention that
> the two queries may return different lists.
> 
> am i missing something? do i follow a bad pattern? why i can't extend
> the context from inside a template?

Does the {% with %} tag help you here?

Something like:

{% with blah.get_some_results as foo %}
  {% if foo %}
 ...
  {% else %}
 ...
  {% endif %}
{% endwith %}

Otherwise, since it's your object, you could always cache the result of
the query in the object.

Cheers,

Chris

--~--~-~--~~~---~--~~
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: assigning variables in templates

2008-10-15 Thread bmt

thanks,
I forgot to mention that these are raw sql queries (because
of aggregates), so the ORM cache won't help me here, but at least now
I understand the reasoning behind the design more.

I used a similar workaround to that what you suggested, so i read
the list out in the view and passed the list to the template.
unfortunately
I still have to pass the object too because of other attributes and
methods,
and for me it seems to be a bit redundant, but it's more like an
aesthetic problem,
and I'm not even sure I'm right.

anyway, thanks again!
bmt
--~--~-~--~~~---~--~~
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: assigning variables in templates

2008-10-15 Thread Malcolm Tredinnick


On Wed, 2008-10-15 at 18:21 -0500, Tim Chase wrote:
> > i have to check the method's return value first with an 'if'
> > tag than run the query again for the 'for' loop. this doubles
> > the load on the db what i don't like, not to mention that the
> > two queries may return different lists.
> 
> Django's ORM should cache your query results, though it may need 
> to be primed with your results first (if the query object doesn't 
> already have results, it issues a COUNT(*) call first, and then 
> your request for results makes for a second call; but if they 
> were done in the other order, the len/bool testing would hit the 
> internal warmed cache first). 

This isn't quite correct. Because the count(*) call could be quite
expensive (any query could be expensive), we avoid making any extra call
like that. Since was already have the queryset at this point, Django
implements a boolean test by reading the first few results from the
result set (i.e. starting to fill the cache). If there are any results,
it can return "True" and since you're almost certain to then actually be
using the queryset, the results are already in memory. It doesn't read
all the results at once, but it reads a chunk (100 at a time).

So it's correct that there will still only be one database call here,
due to the caching nature of querysets. But there will *always* be only
one database call, regardless of when you do the boolean test on the
queryset.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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: assigning variables in templates

2008-10-15 Thread Tim Chase

> i have to check the method's return value first with an 'if'
> tag than run the query again for the 'for' loop. this doubles
> the load on the db what i don't like, not to mention that the
> two queries may return different lists.

Django's ORM should cache your query results, though it may need 
to be primed with your results first (if the query object doesn't 
already have results, it issues a COUNT(*) call first, and then 
your request for results makes for a second call; but if they 
were done in the other order, the len/bool testing would hit the 
internal warmed cache first).  Alternatively, you could force the 
results to be a list before passing it to your render:

   myitems = list(MyModel.objects.filter(...))
   return render_to_response("template.html",
 {"myitems": myitems})

This will allow O(1) calculation of len/bool properties, and only 
hit the DB once.

> am i missing something? do i follow a bad pattern? why i can't
> extend the context from inside a template?

You mostly don't want to try to do this in the template, but want 
to solve the problem in the view.  It helps keep the template 
clean and puts such logic where it belongs.

-tim




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



assigning variables in templates

2008-10-15 Thread bmt

hi,
i'm trying to implement a dynamic table in django.
every column of the table is coded as a python object and i want
to pass a list of these objects to the template as the context.

every object has a method which runs a rather expensive
query on the database and returns a list which can be empty.

my problem is that when this list is empty i want to branch in the
template,
and display some sort of message, instead of the usual 'for' loop
which would
display the returned list. since i can't assign the returned list to a
variable
i have to check the method's return value first with an 'if' tag than
run the query
again for the 'for' loop. this doubles the load on the db what i don't
like, not to mention
that the two queries may return different lists.

am i missing something? do i follow a bad pattern? why i can't extend
the
context from inside a template?

thanks
bmt

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