[Bugfix] 解决批量创建系统用户等资源时,initial_data.get() 失败的问题

pull/3208/head
BaiJiangJie 2019-09-11 14:40:09 +08:00
parent d1f37d4bbf
commit 1eb2038ec4
2 changed files with 14 additions and 2 deletions

View File

@ -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):
"""

View File

@ -42,7 +42,7 @@ class OrgResourceSerializerMixin(serializers.Serializer):
return fields
class BulkOrgResourceSerializerMixin(OrgResourceSerializerMixin, BulkSerializerMixin):
class BulkOrgResourceSerializerMixin(BulkSerializerMixin, OrgResourceSerializerMixin):
pass