[Solved]-Got AttributeError when attempting to get a value for field on serializer

33👍

try this:

serializer = AthleteSerializer(all_athletes, many=True)

8👍

Hasan is right in pointing out many=True.

What it means in DRF is that since your serializer.data is going to be a list and each item in the list needs to be converted to python datatypes that further will be easily rendered into JSON, XML.

Do not be confused with the many-to-many relation.

0👍

When ever you get the "AttributeError" on serializer, always see the last part where it says ‘the original exception was…’. This is why the actual error occurs. E.g

"Got AttributeError when attempting to get a value for field `name` on 
serializer `RegionSerializer`.\nThe serializer field might be named 
incorrectly and not match any attribute or key on the `QuerySet` 
instance.\nOriginal exception text was: 'QuerySet' object has no attribute 
'name'." 

Here, as you can see, the end part which is "QuerySet" object has not attribute xyz .

Hope it is helpful

👤Nomi

Leave a comment