728x90
๋ฐ˜์‘ํ˜•

# 0. Blog ํ”„๋กœ์ ํŠธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

 

url VS DRF url

rest ๋ฐฉ์‹์€ url์— ๋™์ž‘์„ ํ‘œํ˜„ํ•˜์ง€ ์•Š๊ณ  method์—์„œ ํ‘œํ˜„ํ•œ๋‹ค!!

REST๋Š”  url ๋์— / ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๊ฑธ ๊ถŒ์žฅํ•˜์ง€๋งŒ Django์—์„œ๋Š” ๋ถ™์ด๋Š”๊ฑธ ๊ถŒ์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— /๋ฅผ ๋ถ™์ธ๋‹ค...

 

# 1. DRF Example ๋”ฐ๋ผํ•˜๊ธฐ

 

- ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ(python -m venv venv)

 

- router / viewset ์ฝ”๋”ฉํ•˜๊ธฐ

 

1. ๋ ˆ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ์„ค์น˜ํ•˜๊ธฐ : pip install djangorestframework ์„ค์น˜ํ•˜๊ธฐ ๋ฐ‘์— ๋‘๊ฐœ ์•ˆํ•ด๋„ ๋œ๋‹ค!!!

 

2. restframework settings.py์— ๋“ฑ๋กํ•˜๊ธฐ

 

3. urls.py์— rest_framwork.urls ๊ฒฝ๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ(auth๊ด€๋ จ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค)
4. settings.py ์ œ์ผ ๋ฐ‘์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค, Django REST Framework ์„ค์ • ์ค‘ ๊ธฐ๋ณธ ๊ถŒํ•œ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ถ€

 

5. urls.py ์ด๊ฑธ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ!!!

 

DRF ์ฒซํ™”๋ฉด์ด ์ œ๋Œ€๋กœ ๋‚˜์™”์œผ๋ฉด ์„ฑ๊ณต!!!

 

- DRF Browsable API

 

api๋กœ ๋ฐ›๋Š” ๋ฒ„์ „ / default๋Š” api์ด๋‹ค...

 

json์œผ๋กœ ๋ฐ›๋Š” ๋ฒ„์ „

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์„ ์ƒ์† ๋ฐ›์•„ ๋ฉ”์ธ ๊ธฐ๋Šฅ๋“ค์„ ๋‹ค ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค!!!

 

https://www.cdrf.co/

 

Django REST Framework 3.14 -- Classy DRF

What is this? Django REST framework is a powerful and flexible toolkit that makes it easy to build Web APIs. It provides class based generic API views and serializers. We've taken all the attributes and methods that every view/serializer defines or inherit

www.cdrf.co

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)

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts