Skip to content

Instantly share code, notes, and snippets.

@minism
Created November 27, 2012 18:43
Show Gist options
  • Save minism/4156153 to your computer and use it in GitHub Desktop.
Save minism/4156153 to your computer and use it in GitHub Desktop.
Tastypie serializer to return 400 on bad JSON request
from tastypie.serializers import Serializer
from tastypie.exceptions import BadRequest
class JSONClientErrorSerializer(Serializer):
"""
Small override to tastypie's internal serializer to provide a client error
on a malformed JSON request body
"""
def from_json(self, *args, **kwargs):
try:
return super(JSONClientErrorSerializer, self).from_json(*args, **kwargs)
except ValueError:
raise BadRequest("malformed JSON request")
# usage
from tastypie.resources import ModelResource
class MyResource(ModelResource):
class Meta:
serializer = JSONClientErrorSerializer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment