From 909c8a932479424980f14053f8e3820a29aabf11 Mon Sep 17 00:00:00 2001 From: Angelo Date: Fri, 14 Jul 2023 15:50:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20filter=5Ffields=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AD=97=E5=85=B8=E5=8F=8A=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/dvadmin/system/views/user.py | 4 ++-- backend/dvadmin/utils/filters.py | 31 ++++++++-------------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/backend/dvadmin/system/views/user.py b/backend/dvadmin/system/views/user.py index ca39402..ad1bc32 100644 --- a/backend/dvadmin/system/views/user.py +++ b/backend/dvadmin/system/views/user.py @@ -252,12 +252,12 @@ class UserViewSet(CustomModelViewSet): filter_fields = ["^name", "~username", "^mobile", "is_active", "dept", "user_type", "$dept__name"] # filter_fields = { # "name": ["icontains"], - # "mobile": ["icontains"], + # "mobile": ["iregex"], # "username": ["icontains"], - # "gender": ["icontains"], # "is_active": ["icontains"], # "dept": ["exact"], # "user_type": ["exact"], + # "dept__name": ["icontains"], # } search_fields = ["username", "name", "gender", "dept__name", "role__name"] # 导出 diff --git a/backend/dvadmin/utils/filters.py b/backend/dvadmin/utils/filters.py index 0551ba2..88e3523 100644 --- a/backend/dvadmin/utils/filters.py +++ b/backend/dvadmin/utils/filters.py @@ -173,17 +173,6 @@ class CustomDjangoFilterBackend(DjangoFilterBackend): return lookup return None - # CASE: 前端query string过滤 - # def find_filter_lookups(self, orm_lookups, search_term_key): - # for lookup, lookup_expr in orm_lookups.items(): - # # if lookup.find(search_term_key) >= 0: - # search_key = self.construct_search(search_term_key, lookup_expr) - # new_lookup = LOOKUP_SEP.join(search_key.split(LOOKUP_SEP)[:-1]) if len(search_key.split(LOOKUP_SEP)) > 1 else lookup - # # 修复条件搜索错误 bug - # if new_lookup == lookup: - # return search_key - # return None - def get_filterset_class(self, view, queryset=None): """ Return the `FilterSet` class used to filter the queryset. @@ -347,21 +336,19 @@ class CustomDjangoFilterBackend(DjangoFilterBackend): return queryset if filterset.__class__.__name__ == "AutoFilterSet": queryset = filterset.queryset - # orm_lookups = [] - # for search_field in filterset.filters: - # if isinstance(filterset.filters[search_field], CharFilter): - # orm_lookups.append( - # self.construct_search(six.text_type(search_field), filterset.filters[search_field].lookup_expr) - # ) - # else: - # orm_lookups.append(search_field) - orm_lookups = [self.construct_search(str(search_field)) for search_field in self.filter_fields] - # CASE: 前端query string过滤 - # orm_lookups = {lookup: filterset.filters[lookup].lookup_expr for lookup in filterset.filters.keys()} + orm_lookup_dict = dict( + zip( + [field for field in self.filter_fields], + [filterset.filters[lookup].lookup_expr for lookup in filterset.filters.keys()], + ) + ) + orm_lookups = [self.construct_search(lookup, lookup_expr) for lookup, lookup_expr in orm_lookup_dict.items()] + # print(orm_lookups) conditions = [] queries = [] for search_term_key in filterset.data.keys(): orm_lookup = self.find_filter_lookups(orm_lookups, search_term_key) + # print(search_term_key, orm_lookup) if not orm_lookup: continue query = Q(**{orm_lookup: filterset.data[search_term_key]})