How to paginate "Change List" page in Django admin?

37.7k Views Asked by At

With my admin.py below, all items are shown on only one Change List page in Django admin:

admin.py:

from cliente.models import Cliente
from django.contrib import admin

class ClienteAdmin(admin.ModelAdmin):
    list_display = ('nome','bairro','endereco','telefone')
    list_filter = ('bairro',)
    ordering = ('nome',)
    search_fields = ('endereco',)   
    pass

admin.site.register(Cliente,ClienteAdmin)

Now, I want to paginate Change List page to show the specific number of items on each Change List page. So, how can I do this?

4

There are 4 best solutions below

0
On BEST ANSWER

See ModelAdmin.list_per_page. The default is 100, but you can set it to a lower value.

0
On

You can set list_per_page to list the specific number of items on each paginated Change List page. By default, 100 is set to list_per_page.

And, you can also set list_max_show_all to show or hide Show all link on each paginated Change List page. By default, 200 is set to list_max_show_all. *Show all link appears if list_max_show_all value is more than or equal to total items while Show all link disappears if list_max_show_all value is less than total items.

For example, there is Person model below:

# "models.py"

class Person(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name

And, there is Person admin below:

# "admin.py"

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    pass

Then, all 7 persons are listed as shown below:

enter image description here

Now, I set list_per_page = 4 to Person admin as shown below:

# "admin.py"

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_per_page = 4 # Here

Then, 4 persons are listed on the 1st page as shown below:

enter image description here

And, 3 persons are listed on the 2nd page as shown below:

enter image description here

Next, I click on Show all link as shown below:

enter image description here

Then, all 7 persons are listed as shown below:

enter image description here

Next, I set list_max_show_all = 6 to Person admin as shown below:

# "admin.py"

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_per_page = 4
    list_max_show_all = 6 # Here

Then, Show all link disappears as shown below because list_max_show_all value 6 is less than the total persons 7:

enter image description here

0
On

Here I take example of 5 records per page, you can change according to your requirement:

class UserAdmin(admin.ModelAdmin):
    model = User
    list_per_page = 5 # No of records per page 
0
On

Here I take the example of 10 records per page, you can change according to your requirement:

class CK_Sub_CategoryAdmin(admin.ModelAdmin):
   list_display=('Sub_Category_Name','Parent_Category_Name')
   search_fields=('Sub_Category_Name','Parent_Category_Name')
   list_filter=('Sub_Category_Name','SC_Published_Date')
   actions=[make_inactive,make_active]
   list_per_page=10 #record 10 per page