How to implement a paginator that does not call count (*)

I am working on a django site that has a MySQL innodb backend. We have hundreds of thousands of records in several of our tables, and this causes some stability / performance problems for the site in the administrator. In particular, django likes to create count (*) requests when creating paginators, and this causes a lot of problems.

With Django 1.3.x, they began to allow the provision of custom pagination classes. So, I'm interested in finding a way to properly speed up or eliminate these queries. So far I have been browsing these two pages: http://code.google.com/p/django-pagination/source/browse/trunk/pagination/paginator.py https://gist.github.com/1094682 and actually in fact, they did not find what I was looking for. Any suggestions, help, ect. would be highly appreciated.

+8
django admin paginator
source share
2 answers

You can define the _count variable in your paginator

paginator = Paginator(QuerySet, 300) paginator._count = 9000 # or use some query here 

And here is the part of the django paginator code that will help you understand what this variable does and how the page counter works

 def _get_count(self): "Returns the total number of objects, across all pages." if self._count is None: try: self._count = self.object_list.count() except (AttributeError, TypeError): # AttributeError if object_list has no count() method. # TypeError if object_list.count() requires arguments # (ie is of type list). self._count = len(self.object_list) return self._count count = property(_get_count) 
+10
source share

You can also check out django-endless-pagination . endless_pagination.paginator.LazyPaginator nice, but you may need to add a few settings.

0
source share

All Articles