我想根据分页为作者列表生成行号。以下代码显示行号。但是这段代码有一个缺陷:
例如:我在列表中有 100 条记录,我将分页设置为 25,现在我有 4 页。然后我访问了所有 4 页。然后我尝试访问第一页,但它从 101 开始计数,依此类推。有没有办法根据分页值更改代码?
如果我访问第一页,我想要将计数器重置为 1,然后为所有行增加它。
class AuthorAdmin(admin.ModelAdmin):
indexCnt = 0
# If you enable fields variable then you will see only these fields in the editable form
# fields = ['first_name', 'last_name', 'email']
exclude = ['created_date'] # it will exclude this field in the editable field
list_display = ['index_counter', 'first_name', 'last_name', 'gender', 'website', 'email', 'phone_number']
list_display_links = ['first_name', 'last_name']
list_filter = ['gender']
search_fields = ['first_name', 'last_name', 'email', 'summary']
ordering = ['id', 'first_name', 'last_name', 'gender']
list_per_page = 25
def index_counter(self, obj):
self.indexCnt += 1
return self.indexCnt
index_counter.short_description = '#'