Field name `user` is not valid for model `User`.

I am trying to pass in a custom field user_profile to the model serializer, but it keeps showing this error that says Field name useris not valid for modelUser., what could i be possibly doing wrong here.

If i comment out the user_profile function below, everything starts working fine, so i guess there must be something i am missing from there, can anyone please help look into this?

models.py


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(max_length=1000)


class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    title = models.CharField(max_length=500, blank=True ,null=True)
    ...
    likes = models.ManyToManyField(User, blank=True, related_name="likes")

    def __str__(self):
        return self.user.username
    
    @property
    def user_profile(self):
        user_profile = Profile.objects.get(user=self.user)
        return user_profile

Serializer.py


class PostSerializer(serializers.ModelSerializer):
    user_profile = ProfileSerializer(read_only=True)
    
    class Meta:
        model = Post
        fields = ['id','user', 'user_profile' ,'title', 'image', 'video', 'visibility', 'date', 'pid', 'likes']

    def __init__(self, *args, **kwargs):
        super(PostSerializer, self).__init__(*args, **kwargs)
        request = self.context.get('request')
        if request and request.method=='POST':
            self.Meta.depth = 0
        else:
            self.Meta.depth = 3

Views.py

class FriendsPostListView(generics.ListAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = PostSerializer
    
    def get_queryset(self):
        user_id = self.kwargs['user_id']
        user = User.objects.get(id=user_id)
        
        friends = user.profile.friends.all()
        my_posts = Post.objects.filter(user=user)
        friends_posts = Post.objects.filter(user__in=friends)
        all_posts = my_posts | friends_posts
        return all_posts

Your Profile and User models are already in 1-to-1 relationship. So you can always navigate between them with user.profile and profile.user. But in this line:

user_profile = Profile.objects.get(user=self.user)

you are doing a traditional ORM query. Intuitively that looks like it should work but there’s probably some protection against circular logic in place. I bet you can fix it with

user_profile = self.user.profile