Skip to content

Commit 22ffddc

Browse files
6 - Retrieve API View aka Detail View
1 parent c9a249e commit 22ffddc

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/posts/api/serializers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
from posts.models import Post
44

55

6-
class PostSerializer(ModelSerializer):
6+
class PostListSerializer(ModelSerializer):
77
class Meta:
88
model = Post
99
fields = [
10-
'id',
1110
'title',
1211
'slug',
1312
'content',
1413
'publish'
1514
]
1615

1716

17+
class PostDetailSerializer(ModelSerializer):
18+
class Meta:
19+
model = Post
20+
fields = [
21+
'id',
22+
'title',
23+
'slug',
24+
'content',
25+
'publish'
26+
]
27+
1828
""""
1929
2030
data = {

src/posts/api/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from django.contrib import admin
33

44
from .views import (
5+
PostDetailAPIView,
56
PostListAPIView
67
)
78

89
urlpatterns = [
910
url(r'^$', PostListAPIView.as_view(), name='list'),
1011
# url(r'^create/$', post_create),
11-
# url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
12+
url(r'^(?P<slug>[\w-]+)/$', PostDetailAPIView.as_view(), name='detail'),
1213
# url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
1314
# url(r'^(?P<slug>[\w-]+)/delete/$', post_delete),
1415
]

src/posts/api/views.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
from rest_framework.generics import ListAPIView
1+
from rest_framework.generics import ListAPIView, RetrieveAPIView
22

33
from posts.models import Post
4-
from .serializers import PostSerializer
4+
from .serializers import PostDetailSerializer, PostListSerializer
55

6+
7+
class PostDetailAPIView(RetrieveAPIView):
8+
queryset = Post.objects.all()
9+
serializer_class = PostDetailSerializer
10+
lookup_field = 'slug'
11+
#lookup_url_kwarg = "abc"
12+
13+
14+
615
class PostListAPIView(ListAPIView):
716
queryset = Post.objects.all()
8-
serializer_class = PostSerializer
17+
serializer_class = PostListSerializer
918

1019
#def get_queryset()
1120

21+
22+

0 commit comments

Comments
 (0)