扩展Django admin的list_filter()可使用范围方法
作者:kkevinyang 时间:2021-05-01 15:10:47
需求描述
有时候我们会基于已有数据生成一列在表格中,类似于下面的
class BaseSchema(models.Model):
...
def test_status(self):
# pdb.set_trace()
if datetime.date.today() < self.test_start_date:
return '未到测试区间'
elif self.test_end_date and datetime.date.today() > self.test_end_date:
return format_html('<p style="color: red">已下线</p>')
else:
return format_html('<p style="color: green">进行中</p>')
test_status.short_description = u'测试状态'
但同时我们也希望可以对这一列进行筛选,按常规的话也就是添加到list_filter中:
list_filter = ('test_status')
这时候我们会看到django的温馨报错:
The value of 'list_filter[0]' refers to 'test_status', which does not refer to a Field.
也就是说不能使用list_filter对非Field进行筛选。
解决办法
最简单的方法
那就是把这个字段记录进field啊,这样就可以用了。但是我并不想这么做
更高端的方法
参考https://stackoverflow.com/questions/12102697/creating-custom-filters-for-list-filter-in-django-admin/45136544#45136544第二个回答中的事例:
from django.contrib.admin import SimpleListFilter
class CountryFilter(SimpleListFilter):
title = 'country' # or use _('country') for translated title
parameter_name = 'country'
def lookups(self, request, model_admin):
countries = set([c.country for c in model_admin.model.objects.all()])
return [(c.id, c.name) for c in countries] + [
('AFRICA', 'AFRICA - ALL')]
def queryset(self, request, queryset):
if self.value() == 'AFRICA':
return queryset.filter(country__continent='Africa')
if self.value():
return queryset.filter(country__id__exact=self.value())
class CityAdmin(ModelAdmin):
list_filter = (CountryFilter,)
现在我们知道django中是这样实现的筛选的方法,那我们只要覆盖这个方法就好了:
class StatusFilter(SimpleListFilter):
title = 'status'
parameter_name = 'status'
def lookups(self, request, model_admin):
return [(1, '已下线'), (2, '进行中'), (3, '未到测试区间')]
def queryset(self, request, queryset):
this_day = datetime.date.today()
# pdb.set_trace()
if self.value() == '3':
return queryset.filter(test_start_date__gt=this_day)
elif self.value() == '1':
return queryset.filter(test_end_date__lt=this_day)
elif self.value() == '2':
return queryset.filter(test_end_date__gte=this_day, test_start_date__lte=this_day)
然后在添加进list_filter中:
list_filter = (StatusFilter,)
bingo!
来源:https://blog.csdn.net/kkevinyang/article/details/79501576
标签:Django,admin,list,filter
0
投稿
猜你喜欢
ASP实现网页打开任何类型文件都保存的方法
2007-12-21 13:10:00
基于Python实现火车票抢票软件
2021-08-28 18:31:08
python与C、C++混编的四种方式(小结)
2021-06-03 03:01:24
Python自定义主从分布式架构实例分析
2022-09-24 01:28:29
pytorch 批次遍历数据集打印数据的例子
2022-06-09 08:23:46
ASP数据库连接方式大全
2023-07-12 05:52:59
python 字符串的驻留机制及优缺点
2022-11-16 10:07:37
Python可视化tkinter详解
2022-12-31 06:09:19
python sys模块使用方法介绍
2021-11-24 10:02:11
python入门for循环嵌套理解学习
2021-03-01 21:42:16
Javascript的动态增加类的实现方法
2023-10-15 03:03:25
Python调试神器之PySnooper的使用教程分享
2021-12-24 15:37:01
如何将anaconda安装配置的mmdetection环境离线拷贝到另一台电脑
2022-12-16 00:48:47
plsql与tsql的语法不同
2009-09-13 17:33:00
ASP返回某年总共有多少天的代码
2012-12-04 20:36:30
TensorFlow 2.0之后动态分配显存方式
2023-12-20 05:25:17
python字符串str和字节数组相互转化方法
2022-02-14 08:31:48
css清除浮动的最优方法
2008-04-25 22:33:00
python在Windows下安装setuptools(easy_install工具)步骤详解
2022-10-18 19:40:23
详细解读Python中的json操作
2022-02-21 00:55:13