2๐
โ
I think we can drag that paginator into the django 1.10 world (hopefullly not kicking and screaming) like this:
from django.core.paginator import Paginator
from django.core.cache import cache
from django.utils.functional import cached_property
from django.db import connection
class FasterAdminPaginator(Paginator):
@cached_property
def count(self):
try:
if not self.object_list.query.where:
# estimates COUNT: https://djangosnippets.org/snippets/2593/
cursor = connection.cursor()
cursor.execute("SELECT reltuples FROM pg_class WHERE relname = %s",
[self.object_list.query.model._meta.db_table])
print 'Using the reltuples'
ret = int(cursor.fetchone()[0])
else :
return self.object_list.count()
except :
import traceback
traceback.print_exc()
# AttributeError if object_list has no count() method.
return len(self.object_list)
Thakns to cached_property the extensive caching code used in the original snippets are no longer needed. For completeness, this is what the relevent section of django.core.paginator.Paginator looks like
@cached_property
def count(self):
"""
Returns the total number of objects, across all pages.
"""
try:
return self.object_list.count()
except (AttributeError, TypeError):
# AttributeError if object_list has no count() method.
# TypeError if object_list.count() requires arguments
# (i.e. is of type list).
return len(self.object_list)
๐คe4c5
Source:stackexchange.com