Py学习  »  Django

restful api 序列化问题求助

jyt198810 • 6 年前 • 480 次点击  

代码: class CommentSerializer(serializers.ModelSerializer): class Meta: model = Comment fields = ('all') depth = 1 @api_view(['GET']) def comment_list(request): if request.method == "GET": comment_list = Comment.objects.all() serializer = CommentSerializer(comment_list, many=True) return Response(serializer.data)

    设置深度之后可以取出全部,但是指定的话只能取到'belong_user_id'。像这样(fields = ('id','belong_user_id'))。想要取'belong_user_name'这个值,该怎么办?  之前加了外键。

    HTTP 200 OK

Allow: GET, OPTIONS Content-Type: application/json Vary: Accept

[ { "id": 1, "comment": "good", "date": "2018-03-30T09:15:26.892685+08:00", "belong_content": { "id": 1, "title": "第一个标题", "content": "第一篇文章", "date": "2018-03-30T09:15:04.915143+08:00" }, "belong_user": { "id": 1, "password": "safsdfasf", "last_login": "2018-03-30T09:13:59.645474+08:00", "is_superuser": true, "username": "jiang", "first_name": "", "last_name": "", "email": "", "is_staff": true, "is_active": true, "date_joined": "2018-03-30T09:12:51.736376+08:00", "groups": [], "user_permissions": [] } } ]

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/9628
 
480 次点击