Skip to content

Instantly share code, notes, and snippets.

@kimjj81
Created December 6, 2019 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimjj81/91a54d6ce9c47f7b613bbc3b977a0cf3 to your computer and use it in GitHub Desktop.
Save kimjj81/91a54d6ce9c47f7b613bbc3b977a0cf3 to your computer and use it in GitHub Desktop.
Paginator for numerous records in Django-admin.
from django.core.paginator import Paginator
class SimpleCountPaginator(Paginator):
def _get_count(self):
try:
first_id = self.object_list.first().id
last_id = self.object_list.last().id
if (first_id is None) or (last_id is None):
return 0
max_id = max(last_id, first_id)
min_id = min(first_id, last_id)
result = max_id - min_id + 1
return result
except Exception as e:
print(e)
return 0
count = property(_get_count)
@kimjj81
Copy link
Author

kimjj81 commented Dec 6, 2019

from django.contrib import admin

class SomeModelAdmin(admin.ModelAdmin):
paginator = SimpleCountPaginator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment