Skip to content

Instantly share code, notes, and snippets.

@hello-alf
Forked from SeanHayes/api.py
Created June 26, 2016 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hello-alf/574d548c67a2f1d673bfcc2cdfeb60c5 to your computer and use it in GitHub Desktop.
Save hello-alf/574d548c67a2f1d673bfcc2cdfeb60c5 to your computer and use it in GitHub Desktop.
TastyPie: Return 400 Bad Request when deserialization fails
from tastypie import http
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.resources import ModelResource
class CustomModelResource(ModelResource):
def deserialize(self, request, data, format='application/json'):
try:
return super(CustomModelResource, self).deserialize(request, data, format=format)
except Exception as e:
# if an exception occurred here it must be due to deserialization
raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment