开发者

How to mix serializers

开发者 https://www.devze.com 2023-01-23 23:54 出处:网络
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:

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

精彩评论

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