Django suit admin inline pagination

When you have a lot of inline objects, pagination will be a good way to make page of item lighter. You can implement it by yourself. Also keep in mind for Django 1.5 and Django 1.6 implementations differ.

Also currently this code doesn’t support multiple inlines per page, but it can be easy implemented.
Consider this code as a proof of concept, not final code.

Usage:

  • Copy tabular_paginated.html to one of your template directories under admin/edit_inline/ path
  • Use PaginationInline class as base class for your TabularInline

After you setup classes it should look like this:
django suite inline pagination

You need to remove
visibility:hidden
from
.form-horizontal .suit-tab

Template file:
templates/admin/edit_inline/tabular_paginated.html

{% load i18n suit_list %}
{% include 'admin/edit_inline/tabular.html' %}
{% block after_inline %}
    <div class="suit-tab suit-tab-relations">
        {% with inline_admin_formset.formset.cl as cl %}
            {% if cl.paginator %}
                {% pagination cl %}
            {% endif %}
        {% endwith %}
    </div>

    {# Following javaScript is required if you have tabs, 
         otherwise you can remove this JS code #}

    <script type="text/javascript">
        (function ($) {
            Suit.$('.pagination li a').on('click', function () {
                if (document.location.hash) {
                    $(this).attr('href', $(this).attr('href') +
                        '#' + document.location.hash.substr(1));
                }
            });
        }(Suit.$));
    </script>

{% endblock %}

PaginationInline class – Django 1.5.x version

from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from django.core.paginator import EmptyPage, InvalidPage, Paginator

class InlineChangeList(object):
    can_show_all = True
    multi_page = True
    get_query_string = ChangeList.__dict__['get_query_string']

    def __init__(self, request, page_num, paginator):
        self.show_all = 'all' in request.GET
        self.page_num = page_num
        self.paginator = paginator
        self.result_count = paginator.count
        self.params = dict(request.GET.items())

class PaginationInline(admin.TabularInline):
    template = 'admin/edit_inline/tabular_paginated.html'
    per_page = 20

    def get_formset(self, request, obj=None, **kwargs):
        formset_class = super(PaginationInline, self).get_formset(
            request, obj, **kwargs)

        class PaginationFormSet(formset_class):
            def _construct_forms(self, *args, **kwargs):
                qs = self.get_queryset()

                paginator = Paginator(qs, self.per_page)
                try:
                    page_num = int(request.GET.get('p', '0'))
                except ValueError:
                    page_num = 0

                try:
                    page = paginator.page(page_num + 1)
                except (EmptyPage, InvalidPage):
                    page = paginator.page(paginator.num_pages)

                self.cl = InlineChangeList(request, page_num, paginator)
                self.paginator = paginator

                if self.cl.show_all:
                    self._queryset = qs
                else:
                    self._queryset = page.object_list

                return super(PaginationFormSet, self)._construct_forms(
                    *args, **kwargs)

        PaginationFormSet.per_page = self.per_page
        return PaginationFormSet

PaginationInline class – Django 1.6 version

from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from django.core.paginator import EmptyPage, InvalidPage, Paginator

#Use InlineChangeList class from Django 1.5 version code

class PaginationInline(admin.TabularInline):
    template = 'admin/edit_inline/tabular_paginated.html'
    per_page = 20

    def get_formset(self, request, obj=None, **kwargs):
        formset_class = super(PaginationInline, self).get_formset(
            request, obj, **kwargs)
        class PaginationFormSet(formset_class):
            def __init__(self, *args, **kwargs):
                super(PaginationFormSet, self).__init__(*args, **kwargs)

                qs = self.queryset
                paginator = Paginator(qs, self.per_page)
                try:
                    page_num = int(request.GET.get('p', '0'))
                except ValueError:
                    page_num = 0

                try:
                    page = paginator.page(page_num + 1)
                except (EmptyPage, InvalidPage):
                    page = paginator.page(paginator.num_pages)

                self.cl = InlineChangeList(request, page_num, paginator)
                self.paginator = paginator

                if self.cl.show_all:
                    self._queryset = qs
                else:
                    self._queryset = page.object_list

        PaginationFormSet.per_page = self.per_page
        return PaginationFormSet



coded by nessus