如何在Django中将串行器数据转换为dict类型

时间:2018-02-04 19:47:24

标签: python django generics serialization django-rest-framework

>>> from django.core import serializers
>>> from generic.models import banner
>>> query = banner.objects.all()
>>> querydata = serializers.serialize("json",query)
>>> querydata
'[{"model": "generic.banner", "pk": 1, "fields": {"banner": "banner1", "link": null, "category": "bus", "status": "active"}}, {"model": "generic.banner", "pk": 3, "fields": {"banner": "banner2", "link": null, "category": "Mobile", "status": "active"}}]'
>>> type(querydata)
<class 'str'>

在Querydata我得到一个字符串,但我想以下面的格式转换dict中的querydata:

{'bus':{'model': 'generic.banner', 'pk': 1, 'fields': {'banner': 'C:\\wamp\\www\\Paymentapi\\cbanner\\cban_7500.png', 'link': null, 'category': 'bus', 'status': 'active'}}, 'Mobile':{'model': 'generic.banner', 'pk': 3, 'fields': {'banner': 'C:\\wamp\\www\\Paymentapi\\cbanner\\cban_7318.png', 'link': null, 'category': 'Mobile', 'status': 'active'}}}

1 个答案:

答案 0 :(得分:0)

要将字符串转换为字典,只需执行

<TextInput underlineColorAndroid='transparent'
       placeholder="type here ..">
   This is some text you want to render
</TextInput>

现在,import json qs = json.loads(<your string>) query_data = qs[0] 的类型为query_data