Puneet
Puneet

Reputation: 215

Getting attribute error when trying to access the nested serializer in Django Rest Framework

I am getting following error while using the PostSerializer:

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

Serializers are as follows:

class PostSerializer(serializers.ModelSerializer):
    author = UserSerializer(required=False, allow_null=True)
    class Meta:
        model = Post
        fields = ('id', 'author', 'message', 'rating', 'create_date', 'close_date',)

class UserSerializer(serializers.ModelSerializer):
   class Meta:
       model = User
       fields = ('id', 'username', 'full_name',)

View:

class PostMixin(object):
    model = Post
    serializer_class = PostSerializer
    permission_classes = [
        PostAuthorCanEditPermission
    ]
    queryset = model.objects.all()

    def pre_save(self, obj):
        """Force author to the current user on save"""
        obj.author = self.request.user
        return super(PostMixin, self).pre_save(obj)


class PostList(PostMixin, generics.ListCreateAPIView):
    pass

User model:

class User(AbstractBaseUser):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True, null=True)
    full_name = models.CharField(max_length=50, blank=False)
    phone = models.CharField(max_length=20, unique=True, null=True)
    about = models.CharField(max_length=255, blank=True)
    type = models.CharField(max_length=1, default='U')
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['full_name']

    def __unicode__(self):
        return self.email

    def get_full_name(self):
        return self.full_name

    def get_short_name(self):
        return self.full_name

Upvotes: 2

Views: 1816

Answers (2)

bakkal
bakkal

Reputation: 55448

Problem

Got AttributeError when attempting to get a value for field full_name on serializer UserSerializer.

The model User in Django has no such field called full_name. There is though a method get_full_name() that does what you want.

Solution

So try using it through a SerializerMethodField

class UserSerializer(serializers.ModelSerializer):
   class Meta:
       model = User
       fields = ('id', 'username') # no full_name here

   full_name = serializers.SerializerMethodField('get_full_name')

This will add a field called full_name to your serialized object, with the value pulled from User.get_full_name()

Check you are using your custom model and not Django's User model

You've customized your own User model, but since that models has full_name, you shouldn't have gotten that error in the first place, so double check you are not referencing Django's default User model first.

class UserSerializer(serializers.ModelSerializer):
   class Meta:
       model = User # <--- Make sure this is your app.models.User,
                    # and not Django's User model
       fields = ('id', 'username', 'full_name',) # This is OK on your User model

Upvotes: 1

doniyor
doniyor

Reputation: 37846

or just

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name')

Upvotes: 0

Related Questions