Django - REST framework ๋งŒ๋“ค๊ธฐ

๐Ÿ’œpoiuyy0420ยท2021๋…„ 1์›” 17์ผ
1

Django

๋ชฉ๋ก ๋ณด๊ธฐ
1/2
post-thumbnail

๐Ÿ‘ฉ Django REST framework๋ฅผ ์ด์šฉํ•ด RESTful API๋ฅผ ํ•ด๋ณด์•˜๋‹ค.

์šฐ์„  ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๊ณ  ๊ธฐ๋ณธ์ ์ธ Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์ •

๊ฐ€์ƒํ™˜๊ฒฝ venv๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด python์ด 3.x ๋ฒ„์ „ ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
python 2.x ๋ฒ„์ „์—์„œ๋Š” ๊ฐ€์ƒํ™˜๊ฒฝ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— virtualenv ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ๊ทธ ํด๋” ์•ˆ์—์„œ python -m venv ๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„ ์จ์ฃผ๋ฉด ๋œ๋‹ค. dev_env ๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„์„ ๋งŒ๋“ ๋‹ค.

python -m venv dev_env	//window
python3 -m venv dev_env	//mac

๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™”

ํด๋” ์•ˆ์—์„œ ๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„\Scripts\activate.bat ์ด๋ผ๊ณ  ์ณ์ฃผ๋ฉด ๊ฐ€์ƒํ™˜๊ฒฝ์ด ํ™œ์„ฑํ™”๋œ๋‹ค.

dev_env\Scripts\activate	//window
source dev_env/bin/activate	//mac

์„ค์น˜ํ•˜๊ธฐ

๊ฐ€์ƒํ™˜๊ฒฝ ์•ˆ์— Django, REST framework ์„ค์น˜ํ•œ๋‹ค.

(dev_env) pip install django	//Django ์„ค์น˜
(dev_env) pip install djangorestframework	//REST framework ์„ค์น˜

Django ํ”„๋กœ์ ํŠธ

์žฅ๊ณ  ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด django-admin startproject ํ”„๋กœ์ ํŠธ์ด๋ฆ„๋ช…๋ น์–ด๋กœ ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค. ls ๋ช…๋ น์–ด(์œˆ๋„์šฐ : dir)๋กœ ํ™•์ธ์„ ํ•ด๋ณด๋ฉด start_django๋ผ๋Š” ํด๋”๊ฐ€ ์ƒ๊ธด๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

django-admin startproject start_django	//ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

REST framework

Django REST framework๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  settings.py INSTALLED_APPS ์— โ€˜rest_frameworkโ€™๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๋ฉด์„œ ๊ฐ™์ด ์ƒ์„ฑํ•œ โ€˜productโ€™ ์•ฑ๋„ ๊ฐ™์ด ์ถ”๊ฐ€ํ•˜๊ธฐ๐Ÿ˜Š

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'rest_framework',
    'product',
]

Models ์ƒ์„ฑ

start_django/Product/models.py ๋ชจ๋ธ ์ƒ์„ฑํ•˜๊ธฐ

from django.db import models

# Create your models here.

class Product(models.Model):
    name = models.CharField(max_length=256, verbose_name='์ƒํ’ˆ๋ช…')
    price = models.IntegerField(verbose_name='์ƒํ’ˆ๊ฐ€๊ฒฉ')
    description = models.TextField(verbose_name='์ƒํ’ˆ์„ค๋ช…')
    stock = models.IntegerField(verbose_name='์žฌ๊ณ ')
    register_date = models.DateTimeField(auto_now_add=True, verbose_name='๋“ฑ๋ก๋‚ ์งœ')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'fast_product'
        verbose_name = '์ƒํ’ˆ'
        verbose_name_plural = '์ƒํ’ˆ'

Serializers ์ƒ์„ฑ

REST framework ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Serializers๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
Django๋ฅผ ์ด์šฉํ•œ ์›น ๊ฐœ๋ฐœ์—์„œ Django ORM์˜ Queryset์€ Django template๋กœ ๋„˜๊ฒจ์ง€๋ฉฐ HTML๋กœ ๋ Œ๋”๋ง ๋œ๋‹ค. ํ•˜์ง€๋งŒ Serializers๋Š” queryset๊ณผ ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค์™€ ๊ฐ™์€ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ JSON, XML ๋˜๋Š” ๋‹ค๋ฅธ ์ฝ˜ํ…์ธ  ์œ ํ˜•์œผ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๊ธฐ์กด Django template
{% for product in product_list %}
        <tr>
          <th scope="row">{{ product.id }}</th>
          <th>{{ product.name }}</th>
          <th>{{ product.price|intcomma }} ์›</th>
          <th>{{ product.register_date|date:'Y-m-d H:i' }}</th>
        </tr>
{% endfor %}


# JSON ๋ฐ์ดํ„ฐ
{
    "id": 2,
    "name": "๋ฐ”๋‚˜๋‚˜",
    "price": 1000,
    "description": "<p>๋ฐ”๋‚˜๋‚˜์ž…๋‹ˆ๋‹ค</p>",
    "stock": 100,
    "register_date": "2021-01-16T07:13:17.886749Z"
}

Django REST framework๊ฐ€ ์ œ๊ณตํ•˜๋Š” ModelSerializer์„ ์‚ฌ์šฉํ•œ๋‹ค.
product์— serializers.py ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

# start_django/Product/serializers.py

from rest_framework import serializers
from .models import Product

class ProductSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = Product
        fields = '__all__'

Views ์ƒ์„ฑ

View๋Š” ์‹ค์งˆ์ ์œผ๋กœ Queryset์„ ์ปจํŠธ๋กคํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•ด Serializer์„ ํ†ตํ•ด ๋งคํ•‘์‹œ์ผœ ์ค€๋‹ค.

# start_django/Product/views.py

from rest_framework import generics
from rest_framework import mixins
from .models import Product
from .serializers import ProductSerializer

class ProductListAPI(generics.GenericAPIView, mixins.ListModelMixin):
    serializer_class = ProductSerializer

    def get_queryset(self):
        return Product.objects.all().order_by('id')
    
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)


class ProductDeatilAPI(generics.GenericAPIView, mixins.RetrieveModelMixin):
    serializer_class = ProductSerializer

    def get_queryset(self):
        return Product.objects.all().order_by('id')
    
    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

๋งˆ์ง€๋ง‰์œผ๋กœ url.py์— ๋งคํ•‘ ์‹œ์ผœ์ค€๋‹ค.

from django.contrib import admin
from product.views import ProductListAPI, ProductDeatilAPI

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/product/', ProductListAPI.as_view()),
    path('api/product/<int:pk>/', ProductDeatilAPI.as_view())
]

์‹คํ–‰ํ•˜๊ธฐ

Migration ์ƒ์„ฑํ•˜๊ณ  Migrate ์‹คํ–‰ํ•œ๋‹ค.
๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ณ  Migration, Migrate ํ•˜๊ณ  ์ค‘๊ฐ„์— ํ‹ˆํ‹ˆ์ด ํ™•์ธํ–ˆ์—ˆ๋‹ค.

(dev_env) python manage.py makemigrations
(dev_env) python manage.py migrate
(dev_env) python manage.py runserver

๊ทธ๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ๋งŒ๋“  ์ฃผ์†Œ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
ProductListAPI - http://127.0.0.1:8000/api/product/
ProductDeatilAPI - http://127.0.0.1:8000/api/product/2/

Django REST framework๊ฐ€ API ์กฐ์ž‘์„ ๋•๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ํŽ˜์ด์ง€์ด๋‹ค.

profile
์•ˆ๋…•ํ•˜์„ธ์š” :)

0๊ฐœ์˜ ๋Œ“๊ธ€