开发者

How to mix serializers

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:开发者_Go百科

  1. 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
  2. response.transport will 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜