From 37976b11fae1170104d0c1af0d07c01e142de813 Mon Sep 17 00:00:00 2001 From: lukasbuenger Date: Tue, 17 Sep 2013 09:16:08 +0200 Subject: [PATCH] move and extend error raising when determining serializers --- rest_framework/genericrelations.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rest_framework/genericrelations.py b/rest_framework/genericrelations.py index e9c649384..4f5417af5 100644 --- a/rest_framework/genericrelations.py +++ b/rest_framework/genericrelations.py @@ -1,11 +1,9 @@ from __future__ import unicode_literals from django.core.exceptions import ValidationError -from django.core.urlresolvers import get_script_prefix, resolve from django.utils.translation import ugettext_lazy as _ from django import forms -from rest_framework.compat import urlparse from rest_framework import six from rest_framework import serializers from rest_framework.exceptions import ConfigurationError @@ -54,8 +52,6 @@ class GenericRelatedField(serializers.WritableField): def from_native(self, value): # Get the serializer responsible for input resolving serializer = self.determine_serializer_for_data(value) - if serializer is None: - raise ConfigurationError('Could not determine a valid serializer for value "%r"' % value) serializer.initialize(self.parent, self.source) import pdb pdb.set_trace() @@ -73,11 +69,18 @@ class GenericRelatedField(serializers.WritableField): # While one could easily execute the "try" block within from_native and reduce operations, I consider the # concept of serializing is already very naive and vague, that's why I'd go for stringency with the deserialization # process here. + serializers = [] for serializer in six.itervalues(self.serializers): try: serializer.from_native(value) - # Returns the first serializer that can handle the value without errors. - return serializer + # Collects all serializers that can handle the input data. + serializers.append(serializer) except Exception: pass - return None + # If no serializer found, raise error. + l = len(serializers) + if l < 1: + raise ConfigurationError('Could not determine a valid serializer for value "%r"' % value) + elif l > 1: + raise ConfigurationError('There were multiple serializers found for value "%r"' % value) + return serializers[0]