如何将我的用户详细资料的个人资料检索到用户的个人资料页面

时间:2020-05-20 03:19:43

标签: django django-models django-rest-framework django-serializer django-rest-viewsets

我的用户个人资料视图

@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)

当我运行它时说:

“ QuerySet”对象没有属性“作者”

但是当我不使用序列化程序而在shell中运行时,仅使用此代码post=Post.objects.all().filter(author=account),它向我显示了与用户相关的帖子。那么我的序列化器有什么问题?

1 个答案:

答案 0 :(得分:0)

在您的情况下, post QuerySet ,因此您应使用 many=True < / p>

post_serializer=PostSerializer(post, many=True)