File tree Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -194,6 +194,7 @@ $ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutor
194
194
5. [用类视图实现首页 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/94/)
195
195
6. [使用视图集简化代码](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/95/)
196
196
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/)
197
198
198
199
# # 公众号
199
200
< p align=" center" >
Original file line number Diff line number Diff line change 1
1
from django .contrib .auth .models import User
2
2
from rest_framework import serializers
3
3
4
- from .models import Category , Post
4
+ from .models import Category , Post , Tag
5
5
6
6
7
7
class CategorySerializer (serializers .ModelSerializer ):
@@ -22,6 +22,15 @@ class Meta:
22
22
]
23
23
24
24
25
+ class TagSerializer (serializers .ModelSerializer ):
26
+ class Meta :
27
+ model = Tag
28
+ fields = [
29
+ "id" ,
30
+ "name" ,
31
+ ]
32
+
33
+
25
34
class PostListSerializer (serializers .ModelSerializer ):
26
35
category = CategorySerializer ()
27
36
author = UserSerializer ()
@@ -37,3 +46,24 @@ class Meta:
37
46
"author" ,
38
47
"views" ,
39
48
]
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
+ ]
Original file line number Diff line number Diff line change 12
12
from pure_pagination .mixins import PaginationMixin
13
13
14
14
from .models import Category , Post , Tag
15
- from .serializers import PostListSerializer
15
+ from .serializers import PostListSerializer , PostRetrieveSerializer
16
16
17
17
18
18
class IndexView (PaginationMixin , ListView ):
@@ -74,10 +74,21 @@ class IndexPostListAPIView(ListAPIView):
74
74
permission_classes = [AllowAny ]
75
75
76
76
77
- class PostViewSet (mixins .ListModelMixin , viewsets .GenericViewSet ):
77
+ class PostViewSet (
78
+ mixins .ListModelMixin , mixins .RetrieveModelMixin , viewsets .GenericViewSet
79
+ ):
78
80
serializer_class = PostListSerializer
79
81
queryset = Post .objects .all ()
80
82
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
+ )
81
92
82
93
83
94
index = PostViewSet .as_view ({"get" : "list" })
You can’t perform that action at this time.
0 commit comments