extend django-piston to render model form validation messages
By ilgarm | March 2, 2011
I am playing with django-piston for last week or so, and find it simple and nice in most cases I have tried.
I had spent some time though yesterday while trying to return properly rendered django forms validation messages.
I have found couple of solutions, which are fine of course, but one of them had response formats hard coded and actually required piston patching, which I do not want to do, because prefer to stick to official releases, and another one implemented only json rendering, hard coded as well.
Links to the solutions I have found, by the way:
Getting piston forms to play nicely with JSON
django-piston form validation — part 3
So, I basically combined these solutions and investigated a bit piston sources to find how emitters are used in response rendering. I am publishing it here in hope it will be useful for other people, and if so, I will fork piston and send a merge request to master repository. The main benefit of this solution is that it re-uses emitters, so response will be renderer in corresponding format.
One point to mention, this fix assumes write-enabled part of the api is protected with authentication, which is quite reasonable expectation.
This was tested on django-piston 0.2.3.rc1 version.
Create a file in your application, lets say resource.py:
from django.http import HttpResponse
import piston.emitters
import piston.handler
import piston.resource
import piston.utils
class Resource(piston.resource.Resource):
def error_handler(self, e, request, meth, em_format):
if isinstance(e, piston.utils.FormValidationError):
return self.form_validation_response_formatted(request, e, em_format)
return super(Resource, self).error_handler(e, request, meth, em_format)
def form_validation_response_formatted(self, request, e, em_format):
try:
emitter, ct = piston.emitters.Emitter.get(em_format)
fields = self.handler.fields
except ValueError:
result = piston.utils.rc.BAD_REQUEST
result.content = "Invalid output format specified '%s'." % em_format
return result
serialized_errors = dict((key, [unicode(v) for v in values])
for key,values in e.form.errors.items())
srl = emitter(serialized_errors, piston.handler.typemapper, self.handler, fields, False)
stream = srl.render(request)
resp = HttpResponse(stream, mimetype=ct, status=400)
return resp
After that, in your application’s url.py, replace occurrences of piston Resource class to this fixed one.
That is all, it should return properly formatter form validation messages.
Please, if you find it useful let me know. Comments are more than welcome.


2 Comments
Nick on May 17, 2011 at 14:46.
Hi, Thanks for this, It’s exactly what i was looking for. I am having a bit of trouble though… I have followed the post exactly but piston is still outputting html. I’m validating the PUT request using the standard piston @validate decorator. Is that the correct way to do it?
Cheers!
ilgarm on May 18, 2011 at 10:10.
Hi Nick, yes that’s correct. This is what I am doing as well. Can you provide the code snippet of the handler?