List Info

Thread: Counting and indexing objects




Counting and indexing objects
user name
2008-01-03 08:49:13
In my template, i want to be able to print "Item X of Y" where items are got from a QuerySet such as Picture.objects.filter(album=9;name')

Now Y is easy to get, its just the count() of the queryset, however i cant seem to find a solution to find X. The objects do have an ordering set, so they should be in the same order each time the query is run. I know its bad, but i tried to just to a pictures.index(picture) just for testing, but QuerySet doesnt provide that method (sensibly). Any pointers?

Thanks

Tom

--
Tom Badran
http://badrunner.net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Re: Counting and indexing objects
country flaguser name
United States
2008-01-03 09:12:20
> In my template, i want to be able to print "Item X
of Y" where
> items are got from a QuerySet such as
> Picture.objects.filter(album='name')
> 
> Now Y is easy to get, its just the count() of the
queryset,
> however i cant seem to find a solution to find X. The
objects
> do have an ordering set, so they should be in the same
order
> each time the query is run. I know its bad, but i tried
to
> just to a pictures.index(picture) just for testing, but

> QuerySet doesnt provide that method (sensibly). Any
pointers?

It sounds like instead of passing your queryset directly to
the
view/template, you might want to pass
"enumerate(qs)"[1] to your
view/template.  This returns pairs of zero-based indicies
that
you can use for your X.  Use the "add" filter[2]
to make them
1-based indices.

Or more easily, if you're iterating over the list of items
in
your view, you can use the "forloop.counter"
property[3]:

   <dl>
   {% for picture in pictures %}
     <dt>Item {{ forloop.counter }} of {{
pictures.count }}</dt>
     <dd>{}</dd>
   {% endfor %}
   </dl>

-tim

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-24
[2] http://www.djangoproject.com/documentation/templates/#a
dd
[3] http://www.djangoproject.com/documentation/templates/#f
or

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Counting and indexing objects
user name
2008-01-03 09:20:05
Thanks for the hints Tim. The problem is that i'm not using the whole sequence, im just pulling one item out of the query set and was hoping for a way to get the position without having to use the whole set, in the same vain that count() is much more sensible a choice than len() as it gets optimised down to an SQL count by django. Thanks for pointing out enumerate though, im actually embarrassed i didn't know that, it looks very very useful.

Tom

On Jan 3, 2008 3:12 PM, Tim Chase < django.userstim.thechases.com">django.userstim.thechases.com>; wrote:

> In my template, i want to be able to print "Item X of Y" where
> items are got from a QuerySet such as
> Picture.objects.filter(album=9;name')
>
&gt; Now Y is easy to get, its just the count() of the queryset,
> however i cant seem to find a solution to find X. The objects
&gt; do have an ordering set, so they should be in the same order
> each time the query is run. I know its bad, but i tried to
> just to a pictures.index(picture) just for testing, but
> QuerySet doesnt provide that method (sensibly). Any pointers?

It sounds like instead of passing your queryset directly to the
view/template, you might want to pass "enumerate(qs)&quot;[1] to your
view/template.  This returns pairs of zero-based indicies that
you can use for your X.  Use the "add&quot; filter[2] to make them
1-based indices.

Or more easily, if you're iterating over the list of items in
your view, you can use the "forloop.counter" property[3]:

  <dl>
  {% for picture in pictures %}
    <dt>Item {{ forloop.counter }} of {{ pictures.count }}</dt&gt;
    <dd>{}</dd&gt;
  {% endfor %}
  </dl>;

-tim

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-24
[2] http://www.djangoproject.com/documentation/templates/#add
[3] http://www.djangoproject.com/documentation/templates/#for






--
Tom Badran
http://badrunner.net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

how to use random of Built-in filter reference in templates?
country flaguser name
China
2008-01-03 10:09:12
hi ,
we can  see


        "random

Returns a random item from the list."from 
http://www.djangoproject.com/documentation/templates/.b
ut it is diffcult 
to understand without examples.
so,is there anyone who can give me an example about
"random" of Built-in 
filter reference in templates?

thanks!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )