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.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: django-admin-cursor-paginator-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2211896d0b8a101042f6a6ee68879c561c05ca638f9ec4ff124ab28938df700a
MD5 eb3b5a74b4dcbf64912b83200f6522e1
BLAKE2b-256 c43a1222a4f2c2f471673f9bffcda442897a0b4cb80260ec916ec72f9d0dc7d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_admin_cursor_paginator-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 481393e2b899d7fb6386931560f43bf46251515813ac8a48b703887bd1432261
MD5 f76bbdfc1295c98e6bad9f0619e8787e
BLAKE2b-256 58452b8f9bc2172b1cbe3a84b2911694ea0518af35fb46c431185f556f7298cc

See more details on using hashes here.

Supported by

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