From 1eb2038ec47ee544d6b01692a47afcc92c325d40 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Wed, 11 Sep 2019 14:40:09 +0800 Subject: [PATCH] =?UTF-8?q?[Bugfix]=20=E8=A7=A3=E5=86=B3=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E7=AD=89?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=97=B6=EF=BC=8Cinitial=5Fdata.get()=20?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=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/mixins/serializers.py | 14 +++++++++++++- apps/orgs/mixins/serializers.py | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/common/mixins/serializers.py b/apps/common/mixins/serializers.py index f53fc62f4..492a7cf98 100644 --- a/apps/common/mixins/serializers.py +++ b/apps/common/mixins/serializers.py @@ -5,7 +5,7 @@ from django.core.exceptions import ObjectDoesNotExist from rest_framework.utils import html from rest_framework.settings import api_settings from rest_framework.exceptions import ValidationError -from rest_framework.fields import SkipField +from rest_framework.fields import SkipField, empty __all__ = ['BulkSerializerMixin', 'BulkListSerializerMixin'] @@ -36,6 +36,18 @@ class BulkSerializerMixin(object): ret[id_attr] = id_value return ret + def run_validation(self, data=empty): + """ + 批量创建时,获取到的self.initial_data是list, + 所以想用一个属性来存放当前操作的数据集,在validate_field中使用 + :param data: + :return: + """ + # 只有批量创建的时候,才需要重写 initial_data + if self.parent: + self.initial_data = data + return super().run_validation(data) + class BulkListSerializerMixin(object): """ diff --git a/apps/orgs/mixins/serializers.py b/apps/orgs/mixins/serializers.py index ea4e4bd6b..3c039d56f 100644 --- a/apps/orgs/mixins/serializers.py +++ b/apps/orgs/mixins/serializers.py @@ -42,7 +42,7 @@ class OrgResourceSerializerMixin(serializers.Serializer): return fields -class BulkOrgResourceSerializerMixin(OrgResourceSerializerMixin, BulkSerializerMixin): +class BulkOrgResourceSerializerMixin(BulkSerializerMixin, OrgResourceSerializerMixin): pass