# Django Rest Framework?

img

# ๐ŸงฑPreference

Windows10
python 3.7 ์ด์ƒ
Git Bash
Django ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…
HTTP Method


์•ž์„œ ์„ค๋ช…ํ•œ REST์˜ ์žฅ์ ๊ณผ ํ•„์š”์„ฑ์„ ์–ด๋Š์ •๋„ ์ดํ•ดํ•˜์˜€์œผ๋ฉด, ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•ด๋ณด์•„์•ผ๊ฒ ์ฃ .

# 1. DRF(Django Rest Framework)๋ž€?

Django ์•ˆ์—์„œ RESTful API ์„œ๋ฒ„๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

๊ณต์‹๋ฌธ์„œ(opens new window) ์—์„œ ์„ค๋ช…ํ•˜๋Š” Django REST Framework๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์›น ๋ธŒ๋ผ์šฐ์ € API๋Š” ๊ฐœ๋ฐœ์ž์—๊ฒŒ ํฐ ์‚ฌ์šฉ์„ฑ์„ ์ฆ‰ ๊ฐœ๋ฐœ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  • ์ธ์ฆ ์ •์ฑ…์— OAuth1, OAuth2๋ฅผ ์œ„ํ•œ ์ถ”๊ฐ€์ ์ธ ํŒจํ‚ค์ง€๊ฐ€ ์ถ”๊ฐ€ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ
  • ์‹œ๋ฆฌ์–ผ๋ผ์ด์ฆˆ ๊ธฐ๋Šฅ์„ ์ œ๊ณต์„ ํ•ด์ค€๋‹ค. (DB data -> JSON)
  • ๋ฌธ์„œํ™” ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ ์ง€์›์ด ์ž˜ ๋˜์–ด์žˆ๋‹ค.

# 2. Django ๊ธฐ๋ณธ app ๋งŒ๋“ค๊ธฐ

DRF๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์•ž์„œ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ด์ „ ์„ธ์…˜(opens new window) ์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค.

rest_tutorial
โ”œโ”€โ”€ api
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ admin.py
โ”‚   โ”œโ”€โ”€ apps.py
โ”‚   โ”œโ”€โ”€ migrations
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ models.py
โ”‚   โ”œโ”€โ”€ tests.py
โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ””โ”€โ”€ views.py
โ”œโ”€โ”€ db.sqlite3
โ”œโ”€โ”€ manage.py
โ””โ”€โ”€ tutorial
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ settings.py
    โ”œโ”€โ”€ urls.py
    โ””โ”€โ”€ wsgi.py
$ python -m venv myvenv # ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ

$ source myvenv/scripts/activate # ๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰

$ pip install django # django ์„ค์น˜

$ django-admin startproject tutorial #ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

$ python manage.py startapp api # app ์ƒ์„ฑ

# โน 2.1 app ๊ธฐ๋ณธ mapping

์ด์ „ ์„ธ์…˜(opens new window) ์—์„œ ํ–ˆ๋˜ ๋‚ด์šฉ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

โœ ๊ธฐ๋ณธ ์„ค์ • ์‚ดํŽด๋ณด๊ธฐ

# 2.1.1. settings.py ์„ค์ •

rest_tutorial/tutorial/settings.py

INSTALL_APPS = [
	...
    'api', # add
]

# 2.1.2 views.py ์„ค์ •

rest_tutorial/api/views.py

from django.shortcuts import render
from django.http import HttpResponse

def hello(request):
    return HttpResponse("<h1>Hello, world!</h1>")

# 2.1.3 urls.py ์„ค์ •

rest_tutorial/tutorial/urls.py

from django.contrib import admin
from django.urls import path
import api.views   # views.py ์— ์ž‘์„ฑํ•œ hello ํ•จ์ˆ˜ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', api.views.hello, name="hello"),
]

# 2.1.4 Model ์ƒ์„ฑ

rest_tutorial/api/models.py

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    body = models.TextField()

    def __str__(self):
        return self.title

Migration

$ python manage.py makemigrations
$ python manage.py migrate

# 2.1.5 Admin ์ƒ์„ฑ

$ python manage.py createsuperuser

# 2.1.6 Admin์— model ๋“ฑ๋ก

rest_tutorial/api/admin.py

from django.contrib import admin
from .models import Post

admin.site.register(Post)

๊ธฐ๋ณธ์ ์ธ Setting์€ ์ด์ •๋„๋งŒ ํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค.


# 3. Django Rest Framework ์„ค์น˜

Python ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๊ณ  pip๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์น˜ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pip install djangorestframework

# โน 3.1 setting.py ์„ค์ •์— ์ถ”๊ฐ€ํ•˜๊ธฐ

rest_tutorial/tutorial/settings.py

INSTALL_APPS = [
	...
    'api',
    'rest_framework', # add
]

# 4. DRF์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ - Serializer ๋ž€

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ์•Œ์•„๋ณธ Web์ด ์•„๋‹Œ Android App๊ณผ ๊ฐ™์€ ๋ฉ€ํ‹ฐ ํ”Œ๋žซํผ์„ ์ „๋ถ€ ํ•˜๋‚˜์˜ back-end์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” REST์˜ ์žฅ์ ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด JSON ๋“ฑ์˜ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค๊ณ  ํ–ˆ์—ˆ์ฃ !

Serializer๊ฐ€ queryset๊ณผ ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค์™€ ๊ฐ™์€ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ JSON, XML ๋˜๋Š” ๋‹ค๋ฅธ ์ฝ˜ํ…์ธ  ์œ ํ˜•์œผ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๋ฐ›์€ ๋ฐ์ดํ„ฐ์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•œ ๋‹ค์Œ, ๋ณต์žกํ•œ ํƒ€์ž…์œผ๋กœ ํ˜• ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก serializeation์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋ฌผ๋ก  SErializer๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค๊ฐ€ JSON ๋˜๋Š” Dictionary ํ˜•ํƒœ๋กœ ์ง๋ ฌํ™”๋˜๋ฉด ์ข‹์€ ์ ์€ ๋” ๋งŽ์Šต๋‹ˆ๋‹ค!


# โน 4.1 serializers.py ์ƒ์„ฑ

api ํด๋” ์•„๋ž˜์— ๋‹ค์Œ python ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.

rest_tutorial/api/serializers.py

from rest_framework import serializers
from .models import Post

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post  # ๋ชจ๋ธ ์„ค์ •
        fields = ('id', 'title', 'pub_date', 'body')  # ํ•„๋“œ ์„ค์ •

# โน 4.2 views.py ์ž‘์„ฑ

DRF๋Š” ๋ณดํ†ต ์‚ฌ๋žŒ๋“ค์ด ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ณตํ†ต์ ์ธ view ๋กœ์ง์„ ๊ทธ๋ฃนํ™” ํ•œ viewset์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

viewset์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด CRUD ๋กœ์ง์„ ์ง์ ‘ ์งœ์ง€ ์•Š์•„๋„ ์ด ๊ธฐ๋Šฅ๋“ค์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

์•„๋ฌด์ฝ”ํ†  ํ•˜์ง€ ์•Š์•„๋„ CRUD ๊ธฐ๋Šฅ์ด ๋‹ค ๋œ๋‹ค๋Š” ๊ฑฐ์ฃ !

rest_tutorial/api/views.py

from rest_framework import viewsets
from .serializers import ApiSerializer
from .models import Post

class ApiViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = ApiSerializer

# โน 4.3 urls.py ์ž‘์„ฑ

DRF๋Š” url์„ ์ž๋™์œผ๋กœ ๋งตํ•‘ํ•ด์ฃผ๋Š” router๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์ƒ์„ฑํ•œ viewset์„ router์— ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์œผ๋กœ url์„ ๋งตํ•‘ํ•ด์ฃผ์–ด ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ url์„ ๋งตํ•‘ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router.register(r'posts', views.PostViewSet)
# prefix = posts, viewSet = PostViewSet

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(router.urls)),
]

์œ„์—์„œ router.register๋กœ prefix๋ฅผ 'posts'๋กœ ์„ค์ •์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

router๋Š” ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ url์„ ๋งตํ•‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

URL pattern: ^posts/$ Name: 'post-list'

# 5. ์‹คํ–‰ํ•˜๊ธฐ

$ python manage.py runserver

# โน 5.1 ์™„์„ฑ!

๋งŒ๋“  api๋กœ CRUD ์š”์ฒญ์„ ํ•ด๋ด…์‹œ๋‹ค.

api ์š”์ฒญ uri๊ณผ method๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

GET /posts/ : ๊ฒŒ์‹œ๊ธ€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ

POST /posts/ : ๊ฒŒ์‹œ๊ธ€ ์ถ”๊ฐ€

PUT /posts/ : ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •

DELETE /posts/ : ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ


๋จผ์ € localhost:8000 ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ํ™”๋ฉด์ด ๋ณด์ผ ๊ฑฐ์—์š”.

img

posts ๋งํฌ๋กœ ๋“ค์–ด๊ฐ€๋ด…์‹œ๋‹ค.

img

GET /posts/๋Š” ๊ฒŒ์‹œ๊ธ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์š”์ฒญํ•˜๋Š” uri์ด๋‹ค.

์•„๋ž˜ form์œผ๋กœ ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”.

์ž…๋ ฅ ํ•˜๊ณ  post ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด POST ๋ฉ”์†Œ๋“œ๋กœ /posts/ uri์— ์š”์ฒญ์ด ๊ฐ€๊ณ , ํ•ด๋‹น ์ •๋ณด๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

img


๋‚˜๋จธ์ง€ ๊ธฐ๋Šฅ๋„ ํ•œ๋ฒˆ ํ…Œ์ŠคํŠธ ํ•ด๋ณด์„ธ์š”!

DRF๋Š” ๋”์šฑ ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ œ๊ณต๋˜๋‹ˆ๊นŒ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ ํ™œ์šฉํ•˜๋ฉด ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒ ์ฃ !


# Reference

  • https://jamanbbo.tistory.com/43
  • https://www.django-rest-framework.org/tutorial/quickstart/

Last Updated: 9/25/2020, 10:50:12 PM