2

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

9 Comments

I have specified my custom user model and updated the same in question. So, I don't think its still required. Still I tried with your solution and I got the following error:
Error: long object has no attribute username Yes, User model is correct as same serializer is working for User API
Error for username field shows that its a problem for all User fields.
I have included username in UserSerializer only. I have mentioned the view code as well. I think User serializer is not recieving any kind of user information. If is to so, please help me how to pass it to UserSerializer.
Also check Django is using your custom model, settings.AUTH_USER_MODEL = 'myapp.User'
|
0

or just

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.