Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'str' object has no attribute '_meta'

Tags:

python

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?

like image 258
sccrthlt Avatar asked Feb 10 '13 04:02

sccrthlt


People also ask

How do you fix STR has no attribute?

The Python "AttributeError: 'str' object has no attribute" occurs when we try to access an attribute that doesn't exist on string objects. To solve the error, make sure the value is of the expected type before accessing the attribute.

How do I fix AttributeError str object has no attribute decode?

Conclusion # The Python "AttributeError: 'str' object has no attribute 'decode'" occurs when we call the decode() method on a string that has already been decoded from bytes. To solve the error, remove the call to the decode() method as the string is already decoded.

What does STR object has no attribute sort mean?

The Python "AttributeError: 'str' object has no attribute 'sort'" occurs when we try to call the sort() method on a string. To solve the error, use the sorted() function if you need to sort a string alphabetically, or split the words of the string and sort them.


2 Answers

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.

like image 185
icktoofay Avatar answered Oct 22 '22 08:10

icktoofay


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.

like image 23
chirinosky Avatar answered Oct 22 '22 07:10

chirinosky