On 4/10/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> I'm pretty new to Django... At the moment, I'm trying to get all of
> the information stored in a database table into a format acceptable
> for some javascript I plan to run (using the YUI libraries).
>
> I need it to be in a format that looks like this:
>
...
>
> But this gives a type error, saying it's not JSON serializable.

SimpleJSON can't serialize arbitrary Python objects (this includes
Django model instances). Django provides a serialization library
(django.core.serializers) which can serialize model instances, but
this doesn't output in the specific format you require.

You have three options:

1) Write a function that converts the model objects you have into a
python dictionary matching the format you require, then pass the
dictionary through SimpleJson.

2) Use the Django serializer as-is, and convert the JSON structure
into the required format on the client side.

3) Write a custom extension of django.core.serializer.json.Serializer.
As a guideline, you will be need to overwrite the end_object method to
construct Python in the format you require.

Although (3) sounds daunting, it shouldn't be that difficult. The
original implementation (in serializer.base.Serializer) is only 7
lines of code, and pretty self explanatory. In fact, it's very similar
to option (1) - except that the heavy lifting of model traversal has
been done for you.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to