【发布时间】: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