Skip to content

Instantly share code, notes, and snippets.

@SeanHayes
Created July 16, 2012 18:04
Show Gist options
  • Save SeanHayes/3124059 to your computer and use it in GitHub Desktop.
Save SeanHayes/3124059 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