From b6686adef5a349ae04ef46d0f15ae604a2cd49e6 Mon Sep 17 00:00:00 2001 From: chenzuoqing Date: Wed, 8 Dec 2021 11:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=96=B0=E5=A2=9E=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=94=AF=E4=B8=80=E5=AD=97=E6=AE=B5=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/views.py b/src/common/views.py index a346871..90d77d9 100644 --- a/src/common/views.py +++ b/src/common/views.py @@ -15,6 +15,7 @@ class ModelViewBase(Resource): # 是否分页 paging = True # 需要检查重复的字段 + # TODO 可能还需要多个字段组合的判断,并设默认值,dict可能比较合适 uniq_fields = [] # method_decorators 可以用作权限校验,参考 `flask_restful.Resource` @@ -76,10 +77,10 @@ class ModelViewBase(Resource): val = args.get(field) if val: exists = self.model.objects(**{field: val}).first() - # 两种情况需要判断: + # 若存在,以下两种情况需要判断: # 1. 创建时检查内容是否能匹配对象,匹配到返回异常 # 2. 更新时若存在重复,并且不是当前修改的对象,返回异常 - if (not obj and exists) or (obj and obj.id != exists.id): + if exists and ((not obj) or (obj and obj.id != exists.id)): errors.append(f"提交的 {field} 已存在") if errors: abort_response(400, 1001, msg=f"部分字段需要唯一,请检查重复!", errors=errors)