Skip to content

Commit

Permalink
Merge pull request #192 from longguikeji/zzr/get_custom_user
Browse files Browse the repository at this point in the history
fix(get user): 修复自定义字段搜索报错(自定义字段开头为数字)
  • Loading branch information
RockLi authored Jul 10, 2020
2 parents 968e17f + c42c520 commit 612937a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions siteapi/v1/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def get_permissions(self):

# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
def get_queryset(self):
'''
return queryset for list [GET]
Expand Down Expand Up @@ -162,11 +163,14 @@ def get_queryset(self):

# 获取 query string 中自定义字段(*__custom)
# 支持 *__(lte, gte, lt, gt 等)__custom 形式,需进行范围搜索的字段在存储时保证传入的为string
suffix = '__custom'
suffixes = ['__lte__custom', '__lt__custom', '__gte__custom', '__gt__custom', '__custom']
for key, value in self.request.query_params.items():
if key.endswith(suffix):
queryset = queryset.filter(**{'custom_user__data__' + key[:-1 * len(suffix)]: value})

for suffix in suffixes:
if key.endswith(suffix):
_key = 'custom_user__data__"{custom_field}"{suffix}'.format(custom_field=key[:-1 * len(suffix)],
suffix=suffix[:-8])
queryset = queryset.filter(**{_key: value})
break
# 支持自定义排序
# QueryString 中格式为 '&sort=field1 ... fieldn'
_sort = self.request.query_params.get('sort')
Expand Down

0 comments on commit 612937a

Please sign in to comment.