django 通过外键参数筛选

发布于 2023-07-12 11:23:13

@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"],

}

查看更多

关注者
0
被浏览
582
Angelo
Angelo 2023-07-14
这家伙很懒,什么也没写!

简单实现了一版已提交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",
    }
2 个回答
zhaojindong
zhaojindong 2023-07-12
这家伙很懒,什么也没写!

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览