I need to json serialize loads of data in django, and I need to use both django.utils.simplejson and django.core.serializers. The problem is that I can’t mix them. I need something like this:
simplejson.dumps({
'money': money.quantity,
'items_left': item.quantity
'transport': serializers.serialize([transport])
})
While this may work, there still are some problems with it:
- I need to use
[transport]to serialize just one value. I still can’t figure out why the hell couldn’t they add support for serializing a single model response.transportwill be a string that would need another json decode
Also, if it helps, I’m using the wadofstuff json serialization module.
How can I make this kind of serialization less hackish?
Use
.values()in your query in order to generate a dict instead of a model instance, then just nest that dictionary in the outer dictionary.