« RAM-alarm-adingdong / How to memtest on a Mac | »
This is really ugly and you will absolutely need it if you'd like to json serialize form errors in Django. Why? Because you'd like to reply to an AJAX request and just pass trough the errors your form has generated.
simplejson itself is not able to serialize the ErrorDict. But not the ErrorDict itself is the problem - the proxy objects within the ErrorDict are the problem. Those proxy objects represent a string (unicode here) and will be casted whenever needed. This ensures that you won't run into problems with internationalization.
Russell Keith-Magee posted an excellent explanation on a topic dealing with this in 'django users' group.
Also the solution can be found there. I just had to stuff it into a function and now my forms generate error messages that can be understood by e.g. a GWT client.
def error_form_serialization(error_dict):
"""
This method strips the proxy objects from the error dict and casts
them to unicode. After that the error dict can and will be
json-serialized.
"""
plain_dict = dict([(k, [unicode(e) for e in v]) for k,v in error_dict.items()])
return simplejson.dumps(plain_dict)
posted by Jochen Breuer, 2010 February 18
Comments
1 Hermes birkin bag says...
Thank you for useful info. :-)
Posted at 6:11 a.m. on May 10, 2010
2 mbt shoes says...
very well information you write it very clean. I'm very lucky to get this info from you.
Posted at 4:56 a.m. on May 11, 2010
3 patou says...
Short but says it all ! Thanks !
Posted at 5:17 p.m. on May 20, 2010
4 armani sunglass says...
nice to be here.... thanks for share
Posted at 2:12 p.m. on May 24, 2010
5 burberry sunglasses says...
nice share, good
article, very usefull for me...thank you
Posted at 10:44 p.m. on May 24, 2010
6 Dior Sunglass says...
great share, great article, very usefull for me...thank
you
Posted at 6:12 a.m. on May 26, 2010