๐๊ธฐํ
- DRF์ ํ์ด์ง๋ค์ด์
๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ฌ, ์ฝ๊ณ ๋น ๋ฅด๊ฒ API๋ฅผ ๊ตฌํํ๋ค
- ๊ณต์๋ฌธ์์ Viewset์ ํ์ตํ์ฌ, ๊ธฐ์กด์ View, generic View์์ ์ฐจ์ด์ ์ ์์๋ณด์
๐ํ์ต๋ชฉ์
- DRF ๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ฌ View๋ฅผ ๊ตฌํํ๋ค
- ๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ฌ Request์ Response๋ฅผ ๋์ผํ๊ฒ ๊ตฌํํ๋ค
- ๊ณต์๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ Viewset์ผ๋ก ๊ตฌํํ๋ค
๐์ฐธ์กฐ
๐โ๐จDRF VIEWSET
6 - Viewsets and routers - Django REST framework
๐โ๐จDRF ํ์ด์ง๋ค์ด์
Pagination
๐ViewSet๊ตฌํ
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 15,
}
from rest_framework import viewsets
class ArchaivePaginationViewSet(viewsets.ModelViewSet):
queryset = ArchiveModel.objects.all()
serializer_class = ArchiveSerializer
pagination_class = PageNumberPagination
from .views import ArchaivePaginationViewSet
pagination = ArchaivePaginationViewSet.as_view({
'get': 'list',
'post': 'create'
})
urlpatterns = [
path('pagination/', pagination, name='archive_pagination'),
]
๐Generic๊ตฌํ
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 15,
}
urlpatterns = [
path('pagination/', views.ArchaivePagination.as_view(), name='archive_pagination'),
]
class ArchaivePagination(generics.ListAPIView):
queryset = ArchiveModel.objects.all()
serializer_class = ArchiveSerializer
pagination_class = PageNumberPagination
๐ํ์ตํฌ์ธํธ
๐APIView์, Generic View, Viewset์ ์ด๋ค ์ฐจ์ด์ ์ด ์๋๊ฐ?
- DRF์์ ๊ตฌํํ ์ ์๋ View ๊ตฌํ ๋ฐฉ๋ฒ์ APIView, Generic View, Viewset ์ธ ๊ฐ์ง๋ก ๊ตฌ๋ถ๋๋ค.
- APIView๋ RestAPI ๋ฐฉ์์ผ๋ก HTTP Method๋ฅผ ํจ์๋ก ๊ฐ์ง๊ณ ์๋ CBV ํ์์ View ๊ตฌํ ๋ฐฉ๋ฒ์ด๋ค. URL์ View๋ง as_view() ํ์์ผ๋ก ์ฐ๊ฒฐํด๋๋ฉด HTTP Request Method์ ๋ฐ๋ผ์ ํด๋น Method์ ์ด๋ฆ์ ๋ง๋ ํจ์๋ฅผ ํธ์ถํ๋ค
- Generic View๋ ์ด๋ฏธ ๊ตฌํ๋์ด ์๋ View๋ฅผ ์์๋ฐ์ ๊ธฐ๋ฅ์ ๊ทธ๋๋ก ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ด๋ค. ๊ฒ์, ํํฐ ๋ฑ ํน์ API๋ฅผ ์์๋ฐ์ ์ ํด์ง ํ์์ ํ๋๋ฅผ ์์ฑํ๋ ๊ฒ๋ง์ผ๋ก ๋น ๋ฅด๊ฒ View๋ฅผ ๊ตฌํ ๊ฐ๋ฅํ๋ค.
- Viewset์ APIView์ Generic View๋ฅผ ํฉ์น ํํ์ด๋ฉฐ, ๋ฆฌ์คํธ ๋ณด๊ธฐ๋ฅผ ํฌํจํ REST API๋ฅผ ๋ชจ๋ ๋
ํนํ๊ฒ ๊ฐ์ง๊ณ ์๋ ๋ฉ์๋์ ์ฐ๊ฒฐํ์ฌ ๋ชจ๋ ๊ตฌํ์ด ๊ฐ๋ฅํ CBV์ด๋ฉฐ, ๋ํ ๋ฏธ๋ฆฌ ๊ตฌํ๋์ด ์๋ API๋ฅผ ์์ํ๋ ๊ฒ์ผ๋ก ๋น ๋ฅด๊ฒ View๋ฅผ ๊ตฌํํ ์๋ ์๋ค. Viewset์ URL์์ ๋ณ์๋ก ๋ทฐ๋ฅผ as_view() ์์์ list, retrieve, create, update, destroy ๋ฑ์ ๋ฉ์๋์ ๋งคํํ์ฌ ์ ์ธํ ๋ค, UrlPatterns ์์์ ๋ณ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ผ๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
- setting.py ์ REST_FRAMEWORK ์์ ๊ณต์๋ฌธ์ example์ ํ์ฉํ์ฌ ์ฝ๋ ๊ธฐ์ฌ
- ํ์ด์ง๋ค์ด์
์ ์ ์ฉํ ์ฑ์ views.py ๋ก ์ด๋ํ์ฌ ViewSet์ ์์๋ฐ์ ํด๋์ค๋ฅผ ์์ฑํ ํ, ๋ชจ๋ธ๊ณผ ์๋ฆฌ์ผ๋ผ์ด์ , ํ์ด์ง ๋๋ฒ๋ฅผ ๊ธฐ์ฌํ๋ค
- urls.py ์ pagination์ผ๋ก ์ด๋ํ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด์ค์ผ๋ก ํ์ด์ง๋ค์ด์
์ ๊ตฌํํ ์ ์๋ค
๐๊ณต์๋ฌธ์ ์ด๋ป๊ฒ ์ฝ์๋?
- PageNumberPagination์ ์ฐธ์กฐํ์!
ํ์ด์ง๋ค์ด์
API Request&Response
๐ก์! ํ์ด์ง๋ค์ด์
์ ์ด๋ ๊ฒ ์์ฒญํ๊ณ , ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋๊ตฌ๋!
GET https://api.example.org/accounts/?page=4
HTTP 200 OK
{
"count": 1023,
"next": "https://api.example.org/accounts/?page=5",
"previous": "https://api.example.org/accounts/?page=3",
"results": [
โฆ
]
}
ํ์ด์ง๋ค์ด์
์ ๊ตฌํํ๊ธฐ ์ํด ์
์
๐กsettings.py ์์ REST_FRAMEWORK๋ผ๋ ๋ณ์ ์์ ํ์ด์ง๋ค์ด์
์ ๋ฐ๋ก ๋ฃ์ด์ฃผ๋๊ตฌ๋!
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100
}
๊ธฐ๋ณธ์ ์ธ ํ์ด์ง๋ค์ด์
์ฝ๋ ์คํ์ผ
๐ก์ ํ์ด์ง๋ค์ด์
์ ์ด๊ฑธ ๊ฐ์ ธ๋ค๊ฐ ์ฐ๋ฉด ๋๊ฒ ๊ตฌ๋!
- ์ฟผ๋ฆฌ์
์ ์ํ๋ ๋ฐ์ดํฐ ํ์์ด๊ตฌ๋! ๋๋ ์์นด์ด๋ธ๋ชจ๋ธ์ ๊ฐ์ ธ์์ ์ฐ๋ฉด ๋๊ฒ ๋ค!
- ์๋ฆฌ์ผ๋ผ์ด์ ํด๋์ค๋ ์๋ฆฌ์ผ๋ผ์ด์ ๊ฐ์ ธ๋ค ๋ถ์ด๋ฉด ๋๊ตฌ๋!
- ํ์ด์ง๋ค์ด์
ํด๋์ค๋ ์ด๋ฏธ ๋ง๋ค์ด์ง ํ์ด์ง๋ค์ด์
์ ๊ฐ์ ธ๋ค ๋ถ์ด๋ฉด ๋๊ตฌ๋! ๊ณต์๋ฌธ์ ๋ณด๊ณ ํ์ณ์ผ์ง
class BillingRecordsView(generics.ListAPIView):
queryset = Billing.objects.all()
serializer_class = BillingRecordsSerializer
pagination_class = LargeResultsSetPagination
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
from rest_framework.pagination import PageNumberPagination
from rest_framework import generics