Unverified Commit c0afeba2 authored by 老广's avatar 老广 Committed by GitHub

Merge pull request #3208 from jumpserver/dev_serializer

[Bugfix] 解决批量创建系统用户等资源时,initial_data.get() 失败的问题
parents c2ea867d 1eb2038e
...@@ -5,7 +5,7 @@ from django.core.exceptions import ObjectDoesNotExist ...@@ -5,7 +5,7 @@ from django.core.exceptions import ObjectDoesNotExist
from rest_framework.utils import html from rest_framework.utils import html
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from rest_framework.fields import SkipField from rest_framework.fields import SkipField, empty
__all__ = ['BulkSerializerMixin', 'BulkListSerializerMixin'] __all__ = ['BulkSerializerMixin', 'BulkListSerializerMixin']
...@@ -36,6 +36,18 @@ class BulkSerializerMixin(object): ...@@ -36,6 +36,18 @@ class BulkSerializerMixin(object):
ret[id_attr] = id_value ret[id_attr] = id_value
return ret 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): class BulkListSerializerMixin(object):
""" """
......
...@@ -42,7 +42,7 @@ class OrgResourceSerializerMixin(serializers.Serializer): ...@@ -42,7 +42,7 @@ class OrgResourceSerializerMixin(serializers.Serializer):
return fields return fields
class BulkOrgResourceSerializerMixin(OrgResourceSerializerMixin, BulkSerializerMixin): class BulkOrgResourceSerializerMixin(BulkSerializerMixin, OrgResourceSerializerMixin):
pass pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment