'str'对象没有属性'_meta'

时间:2021-12-09 08:02:40
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'

Error: 'str' object has no attribute '_meta'

错误:'str'对象没有'_meta'属性

We're trying to parse the dictionary but it says it's a string? Is it because of the multiple objects in one dictionary?

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

2 个解决方案

#1


12  

json_serializer.serialize is supposed to be used with a queryset. More info here.

json_serializer。serialize应该与queryset一起使用。更多的信息在这里。

You should be able to achieve the same with this:

你应该能够达到同样的目的:

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

Hope this helps.

希望这个有帮助。

#2


7  

Django's serializers are only for serializing QuerySets, but you're passing it a dict. If you want to serialize a dict, perhaps you're looking for Python's built-in json module.

Django的序列化器只用于序列化查询集,但是您将它传递为一个命令。

#1


12  

json_serializer.serialize is supposed to be used with a queryset. More info here.

json_serializer。serialize应该与queryset一起使用。更多的信息在这里。

You should be able to achieve the same with this:

你应该能够达到同样的目的:

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

Hope this helps.

希望这个有帮助。

#2


7  

Django's serializers are only for serializing QuerySets, but you're passing it a dict. If you want to serialize a dict, perhaps you're looking for Python's built-in json module.

Django的序列化器只用于序列化查询集,但是您将它传递为一个命令。