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