JSON enconding result of Model.validate()
Hi, I'm writing a simple ajax form and I'm having trouble JSON-encoding the result of Model.validate(). I always get an error similar to "'This field is required.' cannot be JSON-encoded" To verify the posted data i populate the model I intend to save and call validate(). If there are any errors I want to JSON-encode them and send them back to the client, like this: obj = MyModel(name = request.POST.get('name', False), email = request.POST.get('name', False)) validation_errors = obj.validate() if len(validation_errors) > 0: return HttpResponse(simplejson.dumps(validation_errors), mimetype="application/javascript") I've been searching around and I found that the result of Model.validate() contains some objects of type instance that for some reason cannot be JSON-encoded. I find this all very strange, shouldn't the result of validate() contain string key-pairs? JSON-encoding this should be straightforward. Anyone has encountered this problem or could lend me a hand on this? Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: JSON enconding result of Model.validate()
Thanks, that solved it! On Nov 2, 11:30 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Fri, 2007-11-02 at 10:02 +, grimborg wrote: > > Hi, > > I'm writing a simple ajax form and I'm having trouble JSON-encoding > > the result of Model.validate(). I always get an error similar to > > "'This field is required.' cannot be JSON-encoded" > > > To verify the posted data i populate the model I intend to save and > > call validate(). If there are any errors I want to JSON-encode them > > and send them back to the client, like this: > > > obj = MyModel(name = request.POST.get('name', False), email = > > request.POST.get('name', False)) > > validation_errors = obj.validate() > > if len(validation_errors) > 0: > > return HttpResponse(simplejson.dumps(validation_errors), > > mimetype="application/javascript") > > > I've been searching around and I found that the result of > > Model.validate() contains some objects of type instance that for some > > reason cannot be JSON-encoded. > > > I find this all very strange, shouldn't the result of validate() > > contain string key-pairs? JSON-encoding this should be > > straightforward. > > No, it contains things that are convertable to unicode() on output, but > translated into the users language. They are lazy translation objects. > Call force_unicode() on them to convert them to a real unicode object > (or smart_str() if you need UTF-8 output). > > Regards, > Malcolm > > -- > Depression is merely anger without > enthusiasm.http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: JSON enconding result of Model.validate()
On Fri, 2007-11-02 at 10:02 +, grimborg wrote: > Hi, > I'm writing a simple ajax form and I'm having trouble JSON-encoding > the result of Model.validate(). I always get an error similar to > "'This field is required.' cannot be JSON-encoded" > > To verify the posted data i populate the model I intend to save and > call validate(). If there are any errors I want to JSON-encode them > and send them back to the client, like this: > > obj = MyModel(name = request.POST.get('name', False), email = > request.POST.get('name', False)) > validation_errors = obj.validate() > if len(validation_errors) > 0: > return HttpResponse(simplejson.dumps(validation_errors), > mimetype="application/javascript") > > I've been searching around and I found that the result of > Model.validate() contains some objects of type instance that for some > reason cannot be JSON-encoded. > > I find this all very strange, shouldn't the result of validate() > contain string key-pairs? JSON-encoding this should be > straightforward. No, it contains things that are convertable to unicode() on output, but translated into the users language. They are lazy translation objects. Call force_unicode() on them to convert them to a real unicode object (or smart_str() if you need UTF-8 output). Regards, Malcolm -- Depression is merely anger without enthusiasm. http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: JSON enconding result of Model.validate()
grimborg napisaĆ(a): > I'm writing a simple ajax form and I'm having trouble JSON-encoding > the result of Model.validate(). I always get an error similar to > "'This field is required.' cannot be JSON-encoded" > > To verify the posted data i populate the model I intend to save and > call validate(). If there are any errors I want to JSON-encode them > and send them back to the client, like this: > > obj = MyModel(name = request.POST.get('name', False), email = > request.POST.get('name', False)) > validation_errors = obj.validate() > if len(validation_errors) > 0: > return HttpResponse(simplejson.dumps(validation_errors), > mimetype="application/javascript") > > I've been searching around and I found that the result of > Model.validate() contains some objects of type instance that for some > reason cannot be JSON-encoded. > > I find this all very strange, shouldn't the result of validate() > contain string key-pairs? JSON-encoding this should be > straightforward. I suspect it contains lazy translation of error message. Do a unicode() on it to check. -- Jarek Zgoda Skype: jzgoda | GTalk: [EMAIL PROTECTED] | voice: +48228430101 "We read Knuth so you don't have to." (Tim Peters) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---