Skip to content

Commit 74a684c

Browse files
author
zmrenwu
committed
Step6: 文章详情 API
1 parent 973ba17 commit 74a684c

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ $ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutor
194194
5. [用类视图实现首页 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/94/)
195195
6. [使用视图集简化代码](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/95/)
196196
7. [分页](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/96/)
197+
8. [文章详情 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/97/)
197198

198199
## 公众号
199200
<p align="center">

blog/serializers.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.contrib.auth.models import User
22
from rest_framework import serializers
33

4-
from .models import Category, Post
4+
from .models import Category, Post, Tag
55

66

77
class CategorySerializer(serializers.ModelSerializer):
@@ -22,6 +22,15 @@ class Meta:
2222
]
2323

2424

25+
class TagSerializer(serializers.ModelSerializer):
26+
class Meta:
27+
model = Tag
28+
fields = [
29+
"id",
30+
"name",
31+
]
32+
33+
2534
class PostListSerializer(serializers.ModelSerializer):
2635
category = CategorySerializer()
2736
author = UserSerializer()
@@ -37,3 +46,24 @@ class Meta:
3746
"author",
3847
"views",
3948
]
49+
50+
51+
class PostRetrieveSerializer(serializers.ModelSerializer):
52+
category = CategorySerializer()
53+
author = UserSerializer()
54+
tags = TagSerializer(many=True)
55+
56+
class Meta:
57+
model = Post
58+
fields = [
59+
"id",
60+
"title",
61+
"body",
62+
"created_time",
63+
"modified_time",
64+
"excerpt",
65+
"views",
66+
"category",
67+
"author",
68+
"tags",
69+
]

blog/views.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pure_pagination.mixins import PaginationMixin
1313

1414
from .models import Category, Post, Tag
15-
from .serializers import PostListSerializer
15+
from .serializers import PostListSerializer, PostRetrieveSerializer
1616

1717

1818
class IndexView(PaginationMixin, ListView):
@@ -74,10 +74,21 @@ class IndexPostListAPIView(ListAPIView):
7474
permission_classes = [AllowAny]
7575

7676

77-
class PostViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
77+
class PostViewSet(
78+
mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
79+
):
7880
serializer_class = PostListSerializer
7981
queryset = Post.objects.all()
8082
permission_classes = [AllowAny]
83+
serializer_class_table = {
84+
"list": PostListSerializer,
85+
"retrieve": PostRetrieveSerializer,
86+
}
87+
88+
def get_serializer_class(self):
89+
return self.serializer_class_table.get(
90+
self.action, super().get_serializer_class()
91+
)
8192

8293

8394
index = PostViewSet.as_view({"get": "list"})

0 commit comments

Comments
 (0)