From 21d24ae4bc35bdbee4dfe8c6a24597df66997886 Mon Sep 17 00:00:00 2001 From: "Jiangjie.Bai" Date: Wed, 16 Nov 2022 16:23:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E8=A7=84=E5=88=99=E6=97=B6=E4=B8=8D=E5=8C=85?= =?UTF-8?q?=E5=90=ABactions=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/drf/fields.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/common/drf/fields.py b/apps/common/drf/fields.py index c666d9b76..9742f639c 100644 --- a/apps/common/drf/fields.py +++ b/apps/common/drf/fields.py @@ -4,7 +4,7 @@ import six from django.core.exceptions import ObjectDoesNotExist from django.utils.translation import gettext_lazy as _ from rest_framework import serializers -from rest_framework.fields import ChoiceField +from rest_framework.fields import ChoiceField, empty from common.db.fields import BitChoices from common.utils import decrypt_password @@ -153,3 +153,15 @@ class BitChoicesField(TreeChoicesMixin, serializers.MultipleChoiceField): raise serializers.ValidationError(_("Invalid choice: {}").format(name)) value |= name_value_map[name] return value + + def run_validation(self, data=empty): + """ + 备注: + 创建授权规则不包含 actions 字段时, 会使用默认值(AssetPermission 中设置), + 会直接使用 ['connect', '...'] 等字段保存到数据库,导致类型错误 + 这里将获取到的值再执行一下 to_internal_value 方法, 转化为内部值 + """ + data = super().run_validation(data) + value = self.to_internal_value(data) + self.run_validators(value) + return value