hanleyhansen
hanleyhansen

Reputation: 6452

AttributeError 'list' object has no attribute 'get'

When querying my API with format=json I get the following error:

AttributeError 'list' object has no attribute 'get'

Interestingly enough it doesn't happen with the Django Rest Framework API UI. Only when format=json.

Here is my list method:

def list(self, request):
    queryset = dataset_models.DataSet.objects.all()
    serializer = serializers.DataSetListSerializer(queryset, many=True)
    return Response(serializer.data)

And my serializer:

class DataSetListSerializer(serializers.ModelSerializer):
    class Meta:
        model = dataset_models.DataSet

Traceback:

Traceback:
File "/usr/lib/python2.7/site-packages/Django-1.5.7.example1-py2.7.egg/django/core/handlers/base.py" in get_response
  187.                 response = middleware_method(request, response)

Exception Type: AttributeError at /api/v0/a-cb4be7e8/p/example.com/dataset/
Exception Value: 'list' object has no attribute 'get'

What gives?

Upvotes: 4

Views: 11018

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39581

When you use many=True it serializes several objects and puts them in a list. As the JSONResponse class expects to be passed a dict (unless also passed safe=False), I'm guessing your Response class does as well.

Upvotes: 3

Related Questions