Pagination in Django Admin with Custom List Filters

Viewed 36

I have a proxy model and admin section where I am implementing a custom multi select filter. The results are coming correctly. How ever, pagination is not working with this filter.

http://127.0.0.1:8000/users/?taggroup=1&taggroup=2

When I try to click on page 2 the url turns to :

http://127.0.0.1:8000/users/?p=2&taggroup=2

But the expected result is :

http://127.0.0.1:8000/users/?p=2&taggroup=1&taggroup=2

I tried overriding the change list using [https://stackoverflow.com/questions/62960518/overriding-django-admin-pagination-along-with-url-parameters][1]

But now the pagination is like below when I try navigating from page 1 to page 2 and also the pagination is starting from 0 and not 1:

 http://127.0.0.1:8000/users/?p=2&p=1 

Below is the code that I have tried:

@register.inclusion_tag('admin/custom_pagination.html', takes_context=True)
def custom_pagination(context, cl):
    pagination = admin_list.pagination(cl)
    if 'group_id' in context:
        pagination['params'] = (('taggroup', context['group_id']),)
    return pagination

change_list_template = 'admin/users_changelist.html'

def changelist_view(self, request, extra_context=""):
        response = super(PartnerUsersAdmin, self).changelist_view(
            request, extra_context)
        group_id = request.GET.urlencode()
        if group_id:
            extra_context = {
                'group_id': str(group_id),
            }
            response.context_data.update(extra_context)
        return TemplateResponse(request, "admin/partner_users_changelist.html", response.context_data)

#custom_pagination.html

{% load admin_list %}
{% load i18n %}
{% load content_extras %}
<p class="paginator">
    {% if pagination_required %}
    {% for i in page_range %}
    <a href="?p={{ i }}{% for key, value in params %}{% if key != 'p' %}&{{ value }}{% endif %}{% endfor %}">
        {{i}}</a>
    {% endfor %}
    {% endif %}
    {{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}
    {{ cl.opts.verbose_name_plural }}{% endif %}
    {% if show_all_url %}<a href="{{ show_all_url }}" class="showall">{% trans 'Show all' %}</a>{% endif %}
    {% if cl.formset and cl.result_count %}<input type="submit" name="_save" class="default" value="{% trans 'Save' %}">
    {% endif %}
</p>


#users_changelist.html

Added the below line:

{% block pagination %}{% custom_pagination cl %}{% endblock %}
0 Answers
Related