From 4c64813cdff12969e8ed5320807976ac92674be0 Mon Sep 17 00:00:00 2001 From: Damon Jablons Date: Wed, 4 Nov 2015 12:21:33 -0500 Subject: [PATCH] Work around for #31 --- rest_framework_bulk/drf3/serializers.py | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rest_framework_bulk/drf3/serializers.py b/rest_framework_bulk/drf3/serializers.py index 31fff46..504c88d 100644 --- a/rest_framework_bulk/drf3/serializers.py +++ b/rest_framework_bulk/drf3/serializers.py @@ -35,6 +35,34 @@ def to_internal_value(self, data): class BulkListSerializer(ListSerializer): update_lookup_field = 'id' + def to_internal_value(self, data): + try: + return super(BulkListSerializer, self).to_internal_value(data) + except AttributeError: + pass + + instance_map = { + getattr(i, self.update_lookup_field): i for i in self.instance + } + + ret = [] + errors = [] + for item in data: + field = item[self.update_lookup_field] + self.child.instance = instance_map.get(field) + try: + validated = self.child.run_validation(item) + except ValidationError as exc: + errors.append(exc.detail) + else: + ret.append(validated) + errors.append({}) + + if any(errors): + raise ValidationError(errors) + + return ret + def update(self, queryset, all_validated_data): id_attr = getattr(self.child.Meta, 'update_lookup_field', 'id')