Xavier Ordoquy
2018-05-07 11:02:42 UTC
Validation is only intended from external data (basic Python types) to more complex structures (Django models). This happens during deserialization.
Here, you are already providing a complex model like it was basic Python types.
In order to work this around, it should be:
data = {
âowner_idâ: 1,
âcodeâ: âfooâ
}
ser = SnippetSerializer(data=snippet, many=False)
ser.is_valid(raise_exception=True)
Regards,
Xavier Ordoquy,
Linovia.
Here, you are already providing a complex model like it was basic Python types.
In order to work this around, it should be:
data = {
âowner_idâ: 1,
âcodeâ: âfooâ
}
ser = SnippetSerializer(data=snippet, many=False)
ser.is_valid(raise_exception=True)
Regards,
Xavier Ordoquy,
Linovia.
I checked out the Snippets tutorial and set it up working. Then I tried do a simple validation but I'm getting this error and I'm not sure why.
The serializer is the same from the tutorial: <https://github.com/encode/rest-framework-tutorial/blob/master/snippets/serializers.py>
from snippets.models import *
from snippets.serializers import *
snippet = Snippet()
snippet.owner_id = 1
snippet.code = "foo"
snippet.save()
ser = SnippetSerializer(data=snippet, many=False)
ser.is_valid(raise_exception=True)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-9-087605bd9b37> in <module>()
----> 1 ser.is_valid(raise_exception=True)
~/.virtualenvs/rest/lib/python3.6/site-packages/rest_framework/serializers.py in is_valid(self, raise_exception)
242
--> 244 raise ValidationError(self.errors)
245
246 return not bool(self._errors)
ValidationError: {'non_field_errors': ['Invalid data. Expected a dictionary, but got Snippet.']}
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
The serializer is the same from the tutorial: <https://github.com/encode/rest-framework-tutorial/blob/master/snippets/serializers.py>
from snippets.models import *
from snippets.serializers import *
snippet = Snippet()
snippet.owner_id = 1
snippet.code = "foo"
snippet.save()
ser = SnippetSerializer(data=snippet, many=False)
ser.is_valid(raise_exception=True)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-9-087605bd9b37> in <module>()
----> 1 ser.is_valid(raise_exception=True)
~/.virtualenvs/rest/lib/python3.6/site-packages/rest_framework/serializers.py in is_valid(self, raise_exception)
242
--> 244 raise ValidationError(self.errors)
245
246 return not bool(self._errors)
ValidationError: {'non_field_errors': ['Invalid data. Expected a dictionary, but got Snippet.']}
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.