tags:

views:

229

answers:

2

I have a query that orders a table according to a particular field. The table is related to one competition, so knowing the position is important.

As the table can grow to have a lot of rows, I have paginate it (25 rows per page) using generic views, but the problem I've got is to present the numbered position on the table. This information is generated using a order_by clause.

Result.objects.all().order_by('field')

As I am using generic views to be able to easily paginate, I don't have any view function that allow me to tweak the results, for example using an enumerate

At first, I thought of using forloop.counter to enumerate the positions without the table, but of course the probelm i when you're presenting page 100, you'll still get the numbers as 1,2,3,4,5...25, instead of 2500, 2501, 2502...

I've been seen the annotate() method, to see if I can add to each result the position it occupies on the total, but seems that it only add statistical data like the man and so on...

Is there any way of adding to each result of a query the position it occupies?

A: 

If it's not about identifying rows in the frontend, why not use the primary key of current table?

speakman
Because the primary key is not indicative of the position.
Khelben
+3  A: 

If you're in your template, you can use something like:

{{ forloop.counter0|add:page.start_index }}

Where start_index on pagination's page object gives you the 1-based starting index for that page. Of course, the other way around is probably more readable...

{{ page.start_index|add:forloop.counter0 }}
Will Hardy
Link: http://docs.djangoproject.com/en/1.1/topics/pagination/#page-objects
Felix Kling
Great! it works! ;-)
Khelben