Search code examples
djangodjango-modelsdjango-rest-frameworkdjango-serializerdjango-rest-viewsets

How to retrieve profile of my user details into profile page of user


My view of user profile

@api_view(['GET'])
@permission_classes((IsAuthenticated, ))
def user_profile(request,id):
try:
    up = get_object_or_404(User_Profile, pk=id)
except User_Profile.DoesNotExist:
    return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
    account=request.user
    post=Post.objects.all().filter(author=account)
    post_serializer=PostSerializer(post)
    follower_serializer = FollowerSerializer(up)
    following_serializer = FollowingSerializer(account)
    serializer=[follower_serializer.data,following_serializer.data,post_serializer.data]
    content = {
    'status': 1, 
    'responseCode' : status.HTTP_200_OK, 
    'data': serializer,
    }
    return Response(content)

When I run this it says:

'QuerySet' object has no attribute 'author'

but when I run in shell without using serializer just using this code post=Post.objects.all().filter(author=account) it shows me the posts related to user. So what is problem with my serializer?


Solution

  • In your case, post is a QuerySet, so you should initialize the serializer with many=True

    post_serializer=PostSerializer(post, many=True)