博客五部曲之三 - 博客RESTful


1520 浏览 5 years, 9 months

30 User Detail Serializer

版权声明: 转载请注明出处 http://www.codingsoho.com/

User Detail Serializer

在post和comments里,我们都希望有用户信息显示,可以通过在Serializer里添加user field来实现。
不过在这一节,我们打算专门创建一个user的Serializer,并且将这个Serializer包含在其他的里面,这个是允许的。

首先创建UserDetailSerializer,添加要显示的四个field

src/accounts/api/serializers.py

class UserDetailSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = [
            'username',
            'email',
            'first_name',
            'last_name',
        ]

将这个Serializer添加到post的里面,我们并不打算在这儿修改用户信息,所以添加read_only=True,否则会出现用户信息修改表单

src/posts/api/serializers.py

from accounts.api.serializers import UserDetailSerializer

class PostDetailSerializer(ModelSerializer):
     url = post_detail_url
     # user = SerializerMethodField()
     user = UserDetailSerializer(read_only=True)     
    # def get_user(self, obj):
    #     return str(obj.user.username)


class PostListSerializer(ModelSerializer):
    url = post_detail_url
    # user = SerializerMethodField()
    user = UserDetailSerializer(read_only=True)
    # def get_user(self, obj):
    #     return str(obj.user.username)

访问http://127.0.0.1:8000/api/posts/

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/posts/new-post-1/",
        "title": "new post",
        "publish": "2018-04-18",
        "user": {
            "username": "bhe001",
            "email": "",
            "first_name": "Bin",
            "last_name": "He"
        },
        "delete_url": "[http://127.0.0.1](http://127.0.0.1):8000/api/posts/new-post-1/delete"
    },
    {

查看posts详情 http://127.0.0.1:8000/api/posts/test/

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/posts/test/",
    "title": "test",
    "slug": "test",
    "publish": "2018-04-18",
    "user": {
        "username": "bhe001",
        "email": "",
        "first_name": "Bin",
        "last_name": "He"
    },
    "image": "/media/None/085825680.jpg",
    "content": "test",
    "html": "<p>test</p>\n",
    "comments": [
        {
            "id": 53,
            "content_type": 7,
            "object_id": 5,
            "parent": null,
            "content": "reply ++",
            "reply_count": 1
        },
        {

同样的方法,添加到comments

src/comments/api/serializers.py

from accounts.api.serializers import UserDetailSerializer

class CommentChildSerializer(ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    class Meta:
        model = Comment
        fields = [
            'id',
            'user',
            'content',
            'timestamp',
        ]       

class CommentDetailSerializer(ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    reply_count = SerializerMethodField()
    content_object_url = SerializerMethodField()
    replies =   SerializerMethodField()
    class Meta:
        model = Comment
        fields = [
            'id',
            'user',
            #'content_type',
            #'object_id',
            'content',      

查看评论详情 http://127.0.0.1:8000/api/comments/53

HTTP 200 OK
Allow: GET, PUT, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 53,
    "user": {
        "username": "she001",
        "email": "",
        "first_name": "justin",
        "last_name": "e"
    },
    "content": "reply ++",
    "reply_count": 1,
    "replies": [
        {
            "id": 54,
            "user": {
                "username": "she001",
                "email": "",
                "first_name": "justin",
                "last_name": "e"
            },
            "content": "123",
            "timestamp": "2018-07-18T13:57:07.555000Z"
        }
    ],
    "timestamp": "2018-07-18T13:56:27.753000Z",
    "content_object_url": "/api/posts/test/"
}

在用户登陆序列化里,我们返回了token,这些需要结合前端是实现验证,比如angular,reactjs等