Tip #2: 按你爱好的方法筛选你的数据
很多人应用 Django admin 后台对指定字段进行筛选。要知道,把一个字段名放到 list_filter 列表里就可以了。同时它也异常轻易地创建一个自定义过滤器!
- class ProductiveAuthorsFilter(admin.SimpleListFilter):
- parameter_name = 'is_productive'
- title = 'Productive author'
- YES, NO = 1, 0
- # Number of comments for an author to be considered a productive one
- THRESHOLD = 100
- def lookups(self, request, model_admin):
- return (
- (self.YES, 'yes'),
- (self.NO, 'no'),
- )
- def queryset(self, request, queryset):
- qs = queryset.annotate(Count('comments'))
- # Note the syntax. This way we avoid touching the queryset if our
- # filter is not used at all.
推荐阅读
连接交换机的办法 互联网普及的时代,浩瀚家庭中都有各类上彀的设备,然则平日家里只有一条宽带入户,那么在>>>详细阅读
地址:http://www.17bianji.com/lsqh/35374.html
1/2 1