12
def participant_specific(request, participant):
    helper = RelayFunctions()
    info = helper.participant_specific_donation(participant)
    info1 = helper.participant_specific_milestone(participant)

    data = { 'participant_specific_donation' : info , 'participant_specific_milestone' : info1 }
    json_serializer = serializers.get_serializer("json")()
    response = json_serializer.serialize(data, ensure_ascii=False)
    return HttpResponse(response, mimetype="application/json") 

Traceback:
 File "/home/vtrelayc/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
   111.                         response = callback(request, *callback_args, **callback_kwargs)
 File "/home/vtrelayc/projects/relay/relayapp/views.py" in participant_specific
   192.     response = json_serializer.serialize(data, ensure_ascii=False)
 File "/home/vtrelayc/lib/python2.6/site-packages/django/core/serializers/base.py" in serialize
   46.             concrete_model = obj._meta.concrete_model

 Exception Type: AttributeError at /participants/specific/1/
 Exception Value: 'str' object has no attribute '_meta'

错误:“str”对象没有属性“_meta”

我们正在尝试解析字典,但它说它是一个字符串?是因为一本字典中有多个对象吗?

4

2 回答 2

12

json_serializer.serialize 应该与查询集一起使用。更多信息在这里

您应该能够通过以下方式实现相同的目的:

import json
data = json.dumps({ 'participant_specific_donation' : info , 'participant_specific_milestone' : info1 })

希望这可以帮助。

于 2013-02-10T04:38:27.223 回答
7

Django 的序列化程序仅用于序列化QuerySets,但您将其传递给dict. 如果你想序列化一个dict,也许你正在寻找Python 的内置json模块

于 2013-02-10T04:24:11.357 回答