Django - admin list_filter 自定义过滤器
# admin.py
from django.contrib import admin from django.contrib.admin import SimpleListFilter class MyStatusFilter(SimpleListFilter): # 设置过滤标题 title = "订单状态" # 设置自定义过滤字段 parameter_name = 'status2' # 显示筛选条件 def lookups(self, request, model_admin): return [ (-1, '已取消'), (1, '待付款'), (2, '待收货'), (3, '已完成'), (10, '已退款'), ] def queryset(self, request, queryset): status2 = request.GET.get("status2") # 根据选择条件,自定义最终返回的过滤数据,或使用 Order.objects.filter 进行返回 return queryset.filter(status=status2, is_delete=True) @admin.register(Order) class OrderAdmin(admin.ModelAdmin): list_display = ["id", "user", "status"] list_filter = [MyStatusFilter, ]
最终效果: