AttributeError 'list' object has no attribute 'get'

10,244

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.

Share:
10,244
hanleyhansen
Author by

hanleyhansen

Web and mobile application engineer with a keen interest in all things data. Experience developing in high impact positions on software used by millions of people every day. Software engineering for non-profit religious organization. Python and Swift development.

Updated on June 08, 2022

Comments

  • hanleyhansen
    hanleyhansen almost 2 years

    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?

  • hanleyhansen
    hanleyhansen almost 10 years
    I took another look at the Response class. It's just a subclass of SimpleTemplateResponse. However, I did notice in the documentation that the content_type parameter is supposed to be set automatically if properly determined by content negotiation. In other words, something in the content negotiation seems to be off because if i pass the content_type it works.
  • Denis Cornehl
    Denis Cornehl almost 10 years
    As I recall you can perfectly pass lists to the response-class in DRF.
  • Ross Ridge
    Ross Ridge almost 10 years
    It does seem you're supposed to be able to. A simple way to test whether the list being passed to Response is the list being complained about in the exception would be to make it a dict and see what happens. Eg. Response(dict(enumerate(serializer.data)))
  • hanleyhansen
    hanleyhansen almost 10 years
    @RossRidge Passing a dict works fine. Something like Response({'data': serializer.data}).
  • Ross Ridge
    Ross Ridge almost 10 years
    Have you had any luck trying to get a full traceback? Done the obvious like set DEBUG=True in you Django settings? I think we'll need the full traceback to figure out why it doesn't work with lists.
  • hanleyhansen
    hanleyhansen almost 10 years
    @RossRidge I did paste the full traceback above and debug is set to True. The message is not all that useful.