如何将我的用户详细信息的个人资料检索到用户的个人资料页面
How to retrieve profile of my user details into profile page of user
我对用户个人资料的看法
@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' object has no attribute 'author'
但是当我在 shell 中 运行 而不使用序列化程序时,仅使用此代码 post=Post.objects.all().filter(author=account)
它会向我显示与用户相关的帖子。那么我的序列化程序有什么问题?
在你的例子中,post
是一个 QuerySet,所以你应该用 初始化序列化器many=True
post_serializer=PostSerializer(post, <b>many=True</b>)
我对用户个人资料的看法
@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' object has no attribute 'author'
但是当我在 shell 中 运行 而不使用序列化程序时,仅使用此代码 post=Post.objects.all().filter(author=account)
它会向我显示与用户相关的帖子。那么我的序列化程序有什么问题?
在你的例子中,post
是一个 QuerySet,所以你应该用 初始化序列化器many=True
post_serializer=PostSerializer(post, <b>many=True</b>)