user614778
user614778

Reputation: 577

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

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?

Upvotes: 21

Views: 39793

Answers (4)

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

Upvotes: 2

Shivendra Kumar Singh
Shivendra Kumar Singh

Reputation: 49

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

Upvotes: 4

Rajan Mandanka
Rajan Mandanka

Reputation: 2053

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 

Upvotes: 31

Arnaud
Arnaud

Reputation: 1815

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

Upvotes: 56

Related Questions