List Info

Thread: How do you serialize a newform.errors dictionary?




How do you serialize a newform.errors dictionary?
user name
2007-12-19 17:41:45
This is code that was working until sometime circa rev
6500...

Assuming we have a newform object named form with errors in
it:

import django.utils.simplejson as json
json.dumps(form.errors)

gives:
<type 'exceptions.TypeError'>:
<django.utils.functional.__proxy__
object at 0x128f110> is not JSON serializable

In the shell, and fails silently in view code.

If we do:

json.dumps(force_unicode(form.errors))

the serialization will work, but returns pythonic
representations of
unicode strings (u'foo'), which the JS frontend has no idea
how to
handle.

All I want to do is serialize the form's errors as a
dictionary... I'm
guessing this should be easy?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How do you serialize a newform.errors dictionary?
user name
2007-12-19 18:20:21
EDIT: json.dumps(force_unicode(form.errors)) works but
triggers the
dictionary-to-html conversion.



On Dec 19, 3:41 pm, Andrew <awmccl...gmail.com> wrote:
> This is code that was working until sometime circa rev
6500...
>
> Assuming we have a newform object named form with
errors in it:
>
> import django.utils.simplejson as json
> json.dumps(form.errors)
>
> gives:
> <type 'exceptions.TypeError'>:
<django.utils.functional.__proxy__
> object at 0x128f110> is not JSON serializable
>
> In the shell, and fails silently in view code.
>
> If we do:
>
> json.dumps(force_unicode(form.errors))
>
> the serialization will work, but returns pythonic
representations of
> unicode strings (u'foo'), which the JS frontend has no
idea how to
> handle.
>
> All I want to do is serialize the form's errors as a
dictionary... I'm
> guessing this should be easy?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )