Re: ValidationError in Snippets tutorial

2018-05-07 Thread Xavier Ordoquy
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  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: 
> 
> 
> 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)
>  in ()
> > 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 
> .
> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Custom additional Error Message along with 200 status code

2018-05-07 Thread vallabh vyas
Hi All,

I am new to Django/ Django Rest Framework. I want custom message along with 
custom error message and status code 200 for ValidationError. 

Error message can be.

1. Email not found

2. Wrong password


I had asked this question in stack someone had answered also. but i would 
like to know is there any better way to do that.. below is the stack link

https://stackoverflow.com/questions/50214614/drf-custom-serializer-error-fomat/


Thanks

-- 
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.