11

I get the following error:

Got AttributeError when attempting to get a value for field first_name on serializer AthleteSerializer. The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance. Original exception text was: 'QuerySet' object has no attribute 'first_name'.

Why do I get an error?

This is my views.py:

from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Athlete

from athletics.serializers import AthleteSerializer

class ListAthletes(APIView):
    def get(self, request, format=None):
        all_athletes = Athlete.objects.all()
        import pdb; pdb.set_trace()
        serializer = AthleteSerializer(all_athletes)
        return Response(serializer.data)

This is my serializers.py:

from rest_framework import serializers
from .models import Athlete

class AthleteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Athlete
        fields = (
            'first_name',
            'last_name'
        )

This is my models.py:

from django.db import models

class Athlete(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
user1283776
  • 19,640
  • 49
  • 136
  • 276

3 Answers3

33

try this:

serializer = AthleteSerializer(all_athletes, many=True)
Hasan Ramezani
  • 5,004
  • 24
  • 30
  • 2
    If you don't mind me asking a follow up question. If I am looking to understand any particular Django function and the parameters that should be passed to it, what is a good place to find this? When I google I often end up on the djangoproject.com documentation, which to make seems pretty wordy and I have to read around before I understand which parameters can be passed. – user1283776 Nov 04 '15 at 15:08
  • 2
    @user1283776 , I often look at django documentation and stackoverflow questions. – Hasan Ramezani Nov 04 '15 at 19:55
  • If you're looking for DRF, then here is the best tool I use: http://www.cdrf.co/3.1/rest_framework.serializers/ModelSerializer.html – khashashin Apr 02 '21 at 20:16
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
  • 185
  • 2
  • 13