抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

增加自定义的筛选器

app/admin.py

增加一个只选取月份的筛选器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.contrib import admin
class UpdatedAtFilter(admin.SimpleListFilter):
title = '选择月份'
parameter_name = '月份'

# 菜单列表
def lookups(self, request, model_admin):
"""
List of values to allow admin to select
"""
months = (
(1, '一月'),
(2, '二月'),
(3, '三月'),
(4, '四月'),
(5, '五月'),
(6, '六月'),
(7, '七月'),
(8, '八月'),
(9, '九月'),
(10, '十月'),
(11, '十一月'),
(12, '十二月')
)
return months

def queryset(self, request, queryset):
"""
Return the filtered queryset
"""
if self.value():
# DateTime Field can use __month to get its month value
return queryset.filter(到期日__month=self.value())
else:
return queryset
@admin.register(催费表22年类)
class ********(******):
list_filter = ('***','***','***',UpdatedAtFilter,) #筛选器

评论