Skip to main content

Drop-in replacement for django admin default pagination that works fast with huge tables.

Project description

django-admin-cursor-paginator

Drop-in replacement for django admin default pagination that works fast with huge tables.

Prerequisites

  • You are using django (>2.0) admin
  • Some of your models/tables contains so much data that list page lags
  • You don't want to make separate CRM page for them and just need fast and simple solution
  • You're ok that you won't see total objects or pages count and will navigate only by +-1 page
  • Index must be created for the ordering field, otherwise you won't get any significant speedup (default integer pks work perfectly out-of-box)
  • Ordering field type can be almost anything that can be "encoded" via str() and then be used in queryset.filter()

NOTE
If ordering field is not unique then pagination can mess a bit in edge cases. But for the most often case with datetime fields with milliseconds like created_at it won't be a problem. It can be solved in future by adding support for multiple ordering fields.

Installation

All you need is to install it via pip like that:

pip install django-admin-cursor-paginator

...and extend INSTALLED_APPS (it is needed only for templates):

INSTALLED_APPS = [
    'django.contrib.admin',
    # ...
    'admin_cursor_paginator',
    # ...
]

Usage example

So this app will certainly help you if you're using admin to see some (usually) last inserted entries and may be also want to search for particular objects.

But anyways it's just a regular django admin page.

from admin_cursor_paginator import CursorPaginatorAdmin

class YourModelAdmin(CursorPaginatorAdmin):
    cursor_ordering_field = '-created_at'  # default is '-pk'

class AnotherModelAdmin(CursorPaginatorAdmin):
    cursor_ordering_field = 'parent__sort'  # joins are supported as well

The problem

Default django admin list view makes several queries to show data. The first one is something like select count(*) from app_model to calculate pages range and select * from app_model limit 10 offset 1000 to fetch current page data. Both of them start to produce performance problems as your table grows.

For example, with >1M entries in postgresql table count(*) took a few seconds (in our case) because of seq scan. Limit+offset also performs badly on far pages.

The solution

The solution is to use cursor pagination instead of default one (you can find many standalone packages for it). It doesn't need count(*) query at all and performs fast by using filter instead of limit+offset.

Some useful links:

Test project

In testproject folder you can (surprisingly) find test project that uses this app for one model so that you can see live example and check how it works if you want.

All you need is to install requirements from requirements.txt in some env, run ./manage.py runserver, login as admin:admin to django admin and open Product list.

Project includes sqlite3 file with some test data and preinstalled debug_toolbar to observe db queries.

You can play with ordering field or data amount or may be test it in your target db.

Example of how pagination will look like:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-admin-cursor-paginator-0.1.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file django-admin-cursor-paginator-0.1.1.tar.gz.

File metadata

  • Download URL: django-admin-cursor-paginator-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for django-admin-cursor-paginator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c0f2874d523eee277c804a127e9089c0a5b9f9b0f579dab4951096f13c478b1e
MD5 80d9c285f97a5d4e425516c4326580f3
BLAKE2b-256 ece3c7e6bd2022b112499a3ba430a201c409900c75342b0ee0b087cd1d9d4111

See more details on using hashes here.

File details

Details for the file django_admin_cursor_paginator-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_admin_cursor_paginator-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for django_admin_cursor_paginator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d80b3e4892c2b4543bca5bafccf66a5f4171399cacba472f61e9dfc60b01136e
MD5 aac7fca7fbea61419aee2ad012609ef2
BLAKE2b-256 d20e62ebcf763378e094d907cbf7ea0518a0bf72df3d2469e3f100214283b73d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page