如何对“更改列表”进行分页Django 管理页面?

发布于 2024-10-17 06:06:46 字数 540 浏览 3 评论 0原文

在下面的 admin.py 中,所有项目仅显示在 Django 管理中的一个 更改列表 页面上:

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)

现在,我想要对更改列表页面进行分页,以显示每个更改列表页面上的具体项目数。那么,我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-10-24 06:06:47

这里我以每页10条记录为例,大家可以根据自己的需求进行更改:

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

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
悸初 2024-10-24 06:06:47

您可以设置 list_per_page 列出每个分页更改列表页面上的具体项目数。默认情况下,100 设置为 list_per_page

并且,您还可以设置 list_max_show_all 显示或隐藏每个分页更改列表页面上的显示全部链接。默认情况下,200 设置为 list_max_show_all。 *如果list_max_show_all值大于或等于总项目数,则显示显示全部链接,而如果list_max_show_all则消失,则显示全部链接消失> 价值小于物品总数。

例如,下面有 Person 模型:

# "models.py"

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

    def __str__(self):
        return self.name

并且,下面有 Person admin:

# "admin.py"

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

然后,列出所有 7 人,如下所示:

< a href="https://i.sstatic.net/Hi5FV.png" rel="nofollow noreferrer">输入图像描述这里

现在,我将 list_per_page = 4 设置为 Person admin ,如下所示:

# "admin.py"

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

然后,4 个人会列在第一页如下所示:

在此处输入图像描述

并且,第二页上列出了 3 人,如下所示:

在此处输入图像描述

接下来,我单击 显示全部链接如下所示:

在此处输入图像描述

然后,所有 7 个人如下所示:

在此处输入图像描述

接下来,我设置list_max_show_all = 6Person admin,如下所示:

# "admin.py"

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

然后,显示全部链接消失,如下所示,因为 list_max_show_all6 小于总人数7

< img src="https://i.sstatic.net/AokI9.png" alt="在此处输入图像描述">

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

浊酒尽余欢 2024-10-24 06:06:46

请参阅 ModelAdmin.list_per_page 。默认值为 100,但您可以将其设置为更低的值。

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

美人如玉 2024-10-24 06:06:46

这里我以每页5条记录为例,您可以根据自己的要求进行更改:

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

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文