(tables) Efficient pagination

63 views
Skip to first unread message

dan

unread,
Mar 19, 2009, 9:52:54 AM3/19/09
to Django Apps
Hi,
Thanks for a great django add-on.
But I am a bit puzzled as to how I can limit the rows that I fetch
when doing pagination.

Here's my code:
data = Transaction.objects.filter(**op_kwargs)
transaction = TransactionTable(data, order_by=request.GET.get
('sort'))
transaction.paginate(Paginator, 10, page = 1)

So now in transaction.rows I have all the rows.
And in transaction.rows.pages I have all the paginated rows.

As I will be dealing with huge amount of data. How do I limit all the
rows being fetched?
Thank you.

Michael Elsdörfer

unread,
Mar 19, 2009, 11:29:54 PM3/19/09
to Django Apps
Hi Dan,

> So now in transaction.rows I have all the rows.
> And in transaction.rows.pages I have all the paginated rows.

Probably just a typo on your part, but it should be
"transaction.rows.page".

Now, django-tables really just uses the default pagination tools of
Django here, which in turn rely on the laziness of querysets. It is
true that you have all rows in "transaction.rows" - however, those
should never be loaded from the database unless you actually use the
"rows" property (by iterating over it, indexing into it, querying it's
len etc). If they do, this would be a bug.

So as long as you just use "transaction.rows.page", only the rows from
the current page should be loaded. There's also "transaction.page",
which contains the original Django paginator page object, which you
could use alternatively (e.g. transaction.page.object_list,
transaction.page.has_next etc).

Michael

Daniel Klasson

unread,
Mar 20, 2009, 6:34:12 AM3/20/09
to djang...@googlegroups.com
So the querysets are that lazy, that's awesome. I am just learning
python and django. I used to bang my head with PHP but now I don't think
I can ever go back.

Thanks Michael!

/dan
Reply all
Reply to author
Forward
0 new messages