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.

> Le 7 mai 2018 à 12:36, Rainer Hihn <hihn.rai...@gmail.com> a écrit :
> 
> 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 
>     243         if self._errors and raise_exception:
> --> 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.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-rest-framework+unsubscr...@googlegroups.com 
> <mailto:django-rest-framework+unsubscr...@googlegroups.com>.
> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to