2013-02-10 173 views
8
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' 

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

我们试图解析字典,但它说,它是一个字符串?是否因为一个字典中有多个对象?

回答

11

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

您应该能够达到相同的这一点:

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

希望这有助于。

+1

json.dumps不能很好地与Django查询集合使用 – Coderaemon 2015-12-16 11:57:18

+0

链接是404'ing ...而是发布一段代码以及链接 – surfer190 2017-07-10 11:11:31

6

Django的序列化器仅用于序列化QuerySet s,但是您将它传递给dict。如果你想序列化一个dict,也许你正在寻找Python's built-in json module

+0

谢谢icktoofay!这工作! – Scott 2013-02-10 04:29:04

+0

这是一个很好,直接的答案。谢谢@icktoofay。 – 2013-08-26 16:24:24

相关问题