【问题标题】:How to retrieve profile of my user details into profile page of user如何将我的用户详细信息的个人资料检索到用户的个人资料页面
【发布时间】:2020-05-20 17:56:13
【问题描述】:

我对用户个人资料的看法

@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' 对象没有属性'author'

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

【问题讨论】:

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


    【解决方案1】:

    在您的情况下,post 是一个 QuerySet,因此您应该使用 many=True

    初始化序列化程序
    post_serializer=PostSerializer(post, <b>many=True</b>)

    【讨论】:

    • 谢谢大哥!这是一个小错误,我什至没有想到! :)
    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 2021-05-12
    • 2018-10-16
    • 2011-08-14
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多