'str' object has no attribute '_meta'

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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'

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

回答1:

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

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:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!