Last active
February 25, 2022 11:08
-
-
Save ketanbhatt/846644beb59457485a3decd8dec6a4c4 to your computer and use it in GitHub Desktop.
Large Table Paginator for Django Admin (Accompanying blog: https://medium.com/squad-engineering/estimated-counts-for-faster-django-admin-change-list-963cbf43683e)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.admin import ModelAdmin | |
class MyTableAdmin(ModelAdmin): | |
... | |
paginator = LargeTablePaginator | |
... | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.paginator import Paginator | |
class LargeTablePaginator(Paginator): | |
""" | |
Warning: Postgresql only hack | |
Overrides the count method of QuerySet objects to get an estimate instead of actual count when not filtered. | |
However, this estimate can be stale and hence not fit for situations where the count of objects actually matter. | |
""" | |
def _get_count(self): | |
if getattr(self, '_count', None) is not None: | |
return self._count | |
query = self.object_list.query | |
if not query.where: | |
try: | |
cursor = connection.cursor() | |
cursor.execute("SELECT reltuples FROM pg_class WHERE relname = %s", | |
[query.model._meta.db_table]) | |
self._count = int(cursor.fetchone()[0]) | |
except: | |
self._count = super(LargeTablePaginator, self)._get_count() | |
else: | |
self._count = super(LargeTablePaginator, self)._get_count() | |
return self._count | |
count = property(_get_count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
^ Missing imports:
setup logger from logging as needed