# 0. Blog ํ๋ก์ ํธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
url VS DRF url
rest ๋ฐฉ์์ url์ ๋์์ ํํํ์ง ์๊ณ method์์ ํํํ๋ค!!
REST๋ url ๋์ / ๋ฅผ ๋ถ์ด์ง ์๋๊ฑธ ๊ถ์ฅํ์ง๋ง Django์์๋ ๋ถ์ด๋๊ฑธ ๊ถ์ฅํ๊ธฐ ๋๋ฌธ์ /๋ฅผ ๋ถ์ธ๋ค...
# 1. DRF Example ๋ฐ๋ผํ๊ธฐ
- ๊ฐ์ํ๊ฒฝ ๋ง๋ค๊ธฐ(python -m venv venv)
- router / viewset ์ฝ๋ฉํ๊ธฐ
- DRF Browsable API
DRF default mode
1) browser : api
2) client : json
๋ธ๋ผ์ฐ์ ๊ฐ ์๋๋ผ ๋ค๋ฅธ client๋ผํจ์... curl / httppie / postman / vue.js / react.js...๋ฑ์ด ์๋ค
๊ณต์๋ฌธ์์์๋ api mode == browsable api ๋ผ๊ณ ์ฌ์ฉํ๊ณ ์๋ค!!!
- ์ธ์ฆ ๊ธฐ๋ฅ ์ค๋ฒ๋ทฐ
# DRF
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
settings.py ๋ง์ง๋ง์ ๋ฃ์ ์ด ์ฝ๋์ ์๋ฏธ...
๋ก๊ทธ์ธํ ์ฌ์ฉ์์ ๋ํด์๋ crud๊ฐ ๊ฐ๋ฅ ๊ถํ์ด ์๋ ์ ์ ์ ๋ํ์๋ r๋ง ๊ฐ๋ฅํ๋ค...
์๋ค๋ฉด ๋ชจ๋ ์ฌ์ฉ์์ ๋ํด์ crud๊ฐ ๊ฐ๋ฅํด์ง๋ค..
- DRF ์ฃผ์ ์ฌํญ
/users/ : C, R(list)
/users/pk/ : U, D, R(detail)
/api/users/ : prefix : appName, api
# 2. DRF API ๋ง๋ค๊ธฐ
- url / view / serializer ๊ตฌ์กฐ ์ก๊ธฐ (s, M, u, V ์์ผ๋ก ์ฝ๋ฉ settings / Model / url / View)
urls.py์ ๋ฎ์ด์ด๊ฒ ์๋ก ๋ง๋ api2 ์ฑ์ urls.py / views.py / serializers.py์ ์ ์ ํ ํ ๋นํด์ฃผ๊ธฐ
- DRF Router
DefaultRouter(7๊ฐ) vs SimpleRouter(2๊ฐ)
users/ GET(list)
users/ POST(create)
users/pk/GET(retrieve)
users/pk/PUT(update)
users/pk/DELETE(destroy)
users/pk/PATCH(partial_update)
=======
DefaultRouter์๋ง ์๋๊ฒ๋ค
API Root(api2/)
format suffix(users.json, users.api)
(users/99.json, users/99.api)
ex) users.api, users.json(์ ๋ฏธ์ฌ๋ก ํํ ๊ฐ๋ฅ)
- viewset ๋ฌธ์ ํ์ธ
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
ModelViewSet์ ์์ ๋ฐ์ ๋ฉ์ธ ๊ธฐ๋ฅ๋ค์ ๋ค ๊ตฌํํ ์ ์๋ค!!!
Create => CreateAPIView(POST)
Read => ListAPIView(GET)
=> RetrieveAPIView(GET)
Update => UpdateVPIView(UPDATE, PATCH)
Delete => DestroyAPIView(DELETE)
- blog API ์์ํ๊ธฐ
/api/post/list/ (GET) => /api2/post/ (GET)
/api/post/99/ (GET) => /api2/post/99/ (GET)
/api/comment/create/(POST) => /api2/comment/(POST)
/api/like/99/(GET) =>
/api/catetag/(GET) =>
api2์ ์ด ๊ธฐ๋ฅ๋ค์ DRF๋ก ๋ง๋ค ์์ ์..(like์ catetag๋ ๋์ค์ ๋ณด๊ธฐ๋ก
- viewset์ผ๋ก POST API ๋ง๋ค๊ธฐ
# serializers.py
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
# fields = '__all__'
fields = ['id', 'title', 'image', 'like', 'category']
# urls.py
router.register(r'post', PostViewSet)
#views.py
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
- viewset์ผ๋ก Comment API ๋ง๋ค๊ธฐ
์ ๋ฐฉ๋ฒ๊ณผ ๋์ผ!!!
- viewSet vs Generic Views(์ด ์น๊ตฌ์ ์๋ค)
ListAPIView,CreateAPIView
RetrieveAPIView, UpdateAPIView, DestroyAPIView
/appi2/post/ (GET) --> ListAPIView
/api2/post/99/(GET) --> RetrieveAPIView
/api2/comment/(POST) --> CreateAPIView
# urls.py ๊ฐฑ์ ํ๊ธฐ
from django.urls import path
from api2 import views
urlpatterns = [
path('post/', views.PostListAPIView.as_view(), name='post-list'),
path('post/<int:pk>/', views.PostRetrieveAPIView.as_view(), name='post-detail'),
path('comment/', views.CommentCreateAPIView.as_view(), name='comment-list'),
]
- xxxAPIView ์ธ ModelSerializer
# views.py ๊ฐฑ์
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from api2.serializers import CommentSerializer, PostSerializer
from blog.models import Comment, Post
class PostListAPIView(ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
class PostRetrieveAPIView(RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
class CommentCreateAPIView(CreateAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
Generic View
1) data from db
2) serialize
* ListAPIView => POSTSerializer(instance=, many=True)
* RetrieveAPIView => POSTSerializer(intstance=, many=False)
3) response
- xxxAPIView ์์ค ๊ตฌ์กฐ
# views.py
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from api2.serializers import CommentSerializer, PostListSerializer, PostRetrieveSerializer
from blog.models import Comment, Post
class PostListAPIView(ListAPIView):
queryset = Post.objects.all()
serializer_class = PostListSerializer
class PostRetrieveAPIView(RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostRetrieveSerializer
class CommentCreateAPIView(CreateAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
# serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from blog.models import Comment, Post
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'is_staff']
class PostListSerializer(serializers.ModelSerializer):
class Meta:
model = Post
# fields = '__all__'
fields = ['id', 'title', 'image', 'like', 'category']
class PostRetrieveSerializer(serializers.ModelSerializer):
class Meta:
model = Post
# fields = '__all__'
exclude = ['create_dt']
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
# fields = ['id', 'title', 'image', 'like', 'category']
partial = True
Post(update)
ํน์ ์์ฑ์ ๋ํ update ๊ฐ๋ฅ...
PUT vs PATCH
UpdateAPIView (not PatchAPIView)