[Django]-Raise Validation Error In Pre_Save Using Django Rest Framework

5👍

Use the django rest framework exceptions. For example:

from rest_framework.exceptions import ParseError

...

parsed_data = self.parse(some_data)
if not parsed_data:
    raise ParseError('Some error occurred')

Also note that you won’t see a 404 in there, that’s because it uses the django.http.Http404 exception. These are returned by the API in a nice way.

Note:

If you are doing a significant amount of validation you might want to look at placing your logic in the serializer.

Leave a comment