@liqiang django 通过外键参数筛选 是否支持像search_fields那样,加外键名+双下划线如(dep__name, dep__owner)到filter_fields里实现数据过滤?
filter_fields = {
"name": ["icontains"],
"mobile": ["icontains"],
"username": ["icontains"],
"gender": ["icontains"],
"is_active": ["icontains"],
"dept": ["exact"],
"user_type": ["exact"],
}
search_fields = ["username", "name", "gender", "dept__name", "role__name"]
在filter_fields中加入外键dep的一个属性dep__owner
filter_fields = {
"name": ["icontains"],
"mobile": ["icontains"],
"username": ["icontains"],
"gender": ["icontains"],
"is_active": ["icontains"],
"dept": ["exact"],
"dept__owner": ["exact"],
"user_type": ["exact"],
}
简单实现了一版已提交dev分支, 可以拉取最新代码看一下
filter_fields = ["^name", "~username", "^mobile", "is_active", "dept", "user_type", "$dept__name"]
filter_fields = {
"name": ["icontains"],
"mobile": ["iregex"],
"username": ["icontains"],
"is_active": ["icontains"],
"dept": ["exact"],
"user_type": ["exact"],
"dept__name": ["icontains"],
}
两种配置均可, 列表模式也同时支持配置lookup_prefixes, 类似SearchFilter, 可选参数如下
lookup_prefixes = {
"^": "istartswith",
"=": "iexact",
"@": "search",
"$": "iregex",
"~": "icontains",
}