A
A
axel2018-05-13 03:00:24
Django
axel, 2018-05-13 03:00:24

How to make pagination in Django Admin Inline?

In the admin panel in StackedInline, a very large number of lines are displayed, so it is necessary to do pagination. I'm trying like this:

class CatalogItemInlineAdmin(admin.StackedInline):
    model = CatalogItem
    template = 'admin/edit_inline/list.html'

    def get_formset(self, request, obj=None, **kwargs):
        FormSet = super(CatalogItemInlineAdmin, self).get_formset(request, obj, **kwargs)
        class NewFormSet(FormSet):
            def _construct_forms(self, *args, **kwargs):
                qs = self.get_queryset()
                paginator = Paginator(qs, 20)
                try:
                    page_num = int(request.GET.get('page', '1'))
                except ValueError:
                    page_num = 1
                try:
                    page = paginator.page(page_num)
                except (EmptyPage, InvalidPage):
                    page = paginator.page(paginator.num_pages)
                self.paginator = paginator
                self.page = page
                self._queryset = page.object_list
                self.max_num = len(page.object_list)

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

        return NewFormSet

I do by analogy with the example
https://gist.github.com/coderanger/559911
But absolutely nothing happens. All objects are displayed. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question