Hi, I'm having an issue with the ValidationError, and I think I must be 
doing something wrong.
I'm using a Django management command to retrieve survey data from 
Qualtrics API, and DRF to convert the JSON into Django objects that then 
get saved in the database.  The Qualtrics JSON is very different from the 
db schema / Django structure into which I am importing the data, so I'm not 
using a fair amount of the DRF functionality.  However, it has helped me 
structure things in a familiar way.

This is all working pretty nicely.  However, in the case when I raise an 
exception in the Question.to_internal_value() method, I'm getting a 
ValueError from inside the Collections package (via ReturnDict and 
Serializer.is_valid()).

Here is a simplified version of the code, and the traceback.  Why is the 
ValidationError causing an exception?  
Any help very much appreciated!

In the management command:

serializer = SurveySerializer(data=survey_data)
serializer.is_valid(raise_exception=True)

In serializers.py:

class SurveySerializer(serializers.Serializer):
    def to_internal_value(self, data):
        # lots of code...
        serializer = ModuleSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            output['module_serializer'] = serializer
        else:
            raise serializers.ValidationError(
                "ModuleSerializer invalid: {}".format(serializer.errors))

        # Create the questions, attached to our new Module.  We are passing 
the
        # entire survey data and letting the QuestionSerializer pick out 
and order
        # the questions.
        serializer = QuestionSerializer(data=data, many=True)
        if serializer.is_valid(raise_exception=True):
            output['question_serializer'] = serializer
        else:
            raise serializers.ValidationError("QuestionSerializer invalid")
        # ...
        return output

class QuestionSerializer(serializers.Serializer):
    def to_internal_value(self, data):
        raise serializers.ValidationError('Slider not implemented')
        # ....
        return output

class ModuleSerializer(serializers.Serializer):
    def to_internal_value(self, data):
        return dict(name=data['name'] + ' Module')

When the ValidationError is raised in 
QuestionSerializer.to_internal_value() I get the following traceback:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/django/core/management/__init__.py",
 
line 353, in execute_from_command_line
    utility.execute()
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/django/core/management/__init__.py",
 
line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/django/core/management/base.py",
 
line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/django/core/management/base.py",
 
line 399, in execute
    output = self.handle(*args, **options)
  File 
"/home/liam/Environments/cbe/cbe_survey/surveys/management/commands/qualtrics.py",
 
line 248, in handle
    if serializer.is_valid(raise_exception=True):
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/rest_framework/serializers.py",
 
line 249, in is_valid
    raise ValidationError(self.errors)
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/rest_framework/serializers.py",
 
line 547, in errors
    return ReturnDict(ret, serializer=self)
  File 
"/home/liam/.virtualenvs/cbe/local/lib/python2.7/site-packages/rest_framework/utils/serializer_helpers.py",
 
line 22, in __init__
    super(ReturnDict, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/collections.py", line 69, in __init__
    self.__update(*args, **kwds)
  File "/home/liam/.virtualenvs/cbe/lib/python2.7/_abcoll.py", line 571, in 
update
    for key, value in other:
ValueError: need more than 0 values to unpack






-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to