๐ ์ด ํฌ์คํ ์์๋ Django ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ์ฌ API ์ค๊ณํ๊ณ , ์ด๋ฅผ Create, Readํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์ ๋ฆฌํ์์ต๋๋ค.
๐ฅ API ์์ฑ ๋ฐ ์ฌ์ฉ๋ฒ : POST
๐ฅ API ์์ฑ ๋ฐ ์ฌ์ฉ๋ฒ : GET
๐ฅ ๋ณด์ : List Comprehension ํ์ฉํ์ฌ View ์์ฑํ๊ธฐ
โ๏ธ Server์ HTTP request ๋ฅผ ํ ์ ์๋ ๋ฐฉ๋ฒ์ผ๋ก๋ ์น๋ธ๋ผ์ฐ์ , Httpie, Postman ๋ฑ์ด ์๋ค.
โ๏ธ Httpie ์ค์น ๋ฐฉ๋ฒ์ brew๋ฅผ ์ฌ์ฉํ๋ค.
$ brew install httpie
โ๏ธ httpie๋ฅผ ์ฌ์ฉํ์ฌ POST ์์ฒญ(string type)์ ์๋์ ๊ฐ์ด ํ๋ค. path๋ฅผ ์ ์ ๋ค, key์ value๋ฅผ ๋งคํํ๋ค.
$ http -v POST 127.0.0.1:8000/[path] key1="value1" key2="value2" key3="value3"
โ๏ธ POST ์์ฒญ์ผ๋ก ์จ message body์ ๊ฐ์ DB์ ์์ฑ(CREATE)ํ๊ธฐ ์ํด์๋ ์ฐ์ models.py์ Table์ ๋ง๋ ๋ค.
โ๏ธ Dog ํ ์ด๋ธ์ owner ์ปฌ๋ผ์ ForeignKey๋ก Owner ๋ชจ๋ธ์ ์ฐธ์กฐ์์ผ ์๋ก ๊ด๊ณ๋ฅผ ๋งบ์ด์ค๋ค.
from django.db import models # ๐ Owner table class Owner(models.Model): name = models.CharField(max_length=45) email = models.CharField(max_length=300) age = models.IntegerField() class Meta: db_table = "owners" # ๐ Dog table class Dog(models.Model): name = models.CharField(max_length=45) age = models.IntegerField() owner = models.ForeignKey("Owner", on_delete=models.CASCADE) class Meta: db_table = "dogs"
โ๏ธ Python์ Dictํ ์๋ฃ๊ตฌ์กฐ๊ฐ ์์ง๋ง, ๋ฐ๋ก JOSNํ์์ ์ดํดํ ์ ์๋ค. ์ด์ HTTP Message์ body ๋ถ๋ถ์ json.loads๋ฅผ ํตํด Dict๋ก ๋ณํ์์ผ Python์์ ์ฒ๋ฆฌํด์ค๋ค.
โ๏ธ ์ฒ๋ฆฌ ํ ๋ค์ JOSN ํ์์ผ๋ก ๊ฐ์ ๋ฐํํ๊ธฐ ์ํด JSONResponse ๋ชจ๋์ ์ด์ฉํ๋ค.
โ๏ธ ์ฑ๊ณต์ ์๋ฏธํ๋ Status Code ์ค, ์์ฑ์ 201๋ฒ์ผ๋ก ๋ฐํํ๋ค.
โ๏ธ ์ฆ, JSONํ์์ request๋ฅผ ๋ฐ์์ฌ ๋๋ json.loads()
๋ค์ JSONํ์์ผ๋ก response๋ฅผ ๋ฐํํ ๋๋ JsonResponse๋ฅผ ์ฌ์ฉํ๋ค.
import json from django.http import JsonResponse from django.views import View from .models import Owner, Dog # ๐ OwnerView controller class OwnerView(View): def post(self, request): try: data = json.loads(request.body) Owner.objects.create( name=data["name"], email=data["email"], age=data["age"] ) except KeyError: return JsonResponse({"MESSAGE": "IVALID_KEY"}, status=400) return JsonResponse({"MESSAGE": "CREATED"}, status=201) # ๐ DogView controller class DogView(View): def post(self, request): try: data = json.loads(request.body) Dog.objects.create( name=data["name"], age=data["age"], owner=Owner.objects.get(name=data["owner"]), ) except KeyError: return JsonResponse({"MESSAGE": "IVALID_KEY"}, status=400) return JsonResponse({"MESSAGE": "CREATED"}, status=201)
โ๏ธ ํด๋ผ์ด์ธํธ๋ URI ํตํด ์๋ฒ์ ์์ฒญํ๋๋ฐ ์ด ๊ฒฝ๋ก๋ ์์์ ์ ํํ ์๋ณํ ์ ์๋๋ก ๊ตฌ์ฑ๋์ด์ผ ํ๋ค. ํ์๋ ๋งค์๋(GET, POST..)๋ก ๊ตฌ๋ถํ๊ธฐ ๋๋ฌธ์ ์ต๋ํ ์์์ ๋ช ํํ ๋ํ๋ด๋๋ก URL๋ฅผ ์ค๊ณํด์ผ ํ๋ค.
โ๏ธ ๋ฉ์ธ urls.py์์ owners ์ฑ์ urls.py๋ก ์ฒ๋ฆฌ๋ฅผ ์์ํ๋๋ก include๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ ํ๋ค.
from django.urls import path, include urlpatterns = [ path("", include("owners.urls")), ]
โ๏ธ OwnerView์ DogView์ ๋ํ path๋ ์๋์ ๊ฐ์ด ์ง์ ํด์ค๋ค.
from django.urls import path from .views import OwnerView, DogView urlpatterns = [ path("owner/", OwnerView.as_view()), path("dog/", DogView.as_view()), ]
โ๏ธ ์ด์ key&value ํํ๋ก Httpi ์์ฒญ(POST)์ ๋ณด๋ด๋ฉด, MySQL์ DB์ ๊ฐ์ด ์ ์ฅ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋จ, Controller๊ฐ ์๋ํ ์ ์๋๋ก urls.py์ path๋ฅผ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด์ฃผ์ด์ผ ํ๋ค.
$ http -v POST 127.0.0.1:8000/owner name="์ฅ์ฌ์" age=33 email="jewon119@gmail.com"
$ http -v POST 127.0.0.1:8000/dog name="๋ณต์ค์ด" age=7 owner="์ฅ์ฌ์"
โ๏ธ HTTP ๋งค์๋๊ฐ POST๋ก ์์ฒญ์ฌ ๋๋ ํด๋น CBV(Class-based-View)์ post ๋งค์๋๊ฐ ์คํ๋๊ณ , GET ๋งค์๋๋ก ์์ฒญ์จ๋ค๋ฉด CBV์ get ๋งค์๋๊ฐ ์๋ํ๋ค.
โ๏ธ DB์์ ๋ฐ์ดํฐ์ Dictํ์ผ๋ก ์ฒ๋ฆฌํ ํ, ๊ฐ๊ณตํ ๋ฐ์ดํฐ๋ฅผ JsonResponse์ ์ฌ์ฉํด JSONํ์์ผ๋ก ํด๋ผ์ด์ธํธ๋ก ๋ฐํํด์ฃผ๋ฉด ๋๋ค.
import json from django.http import JsonResponse from django.views import View from .models import Owner, Dog # ๐ OwnerView controller class OwnerView(View): def post(self, request): # ๐ POST ์์ฒญ์ผ ๋ ์๋ ......์๋ต...... def get(self, request): # ๐ GET ์์ฒญ์ผ ๋ ์๋ owners = Owner.objects.all() result = [] for owner in owners: dogs = owner.dog_set.all() dog_list = [] for dog in dogs: dog_data = { "name": dog.name, "age": dog.age, } dog_list.append(dog_data) result.append( { "name": owner.name, "mail": owner.email, "age": owner.age, "my_dogs": dog_list, } ) return JsonResponse({"MESSAGE": result}, status=200) # ๐ DogView controller class DogView(View): def post(self, request): ......์๋ต...... def get(self, request): dogs = Dog.objects.all() result = [] for dog in dogs: result.append( { "name": dog.name, "age": dog.age, "owner": dog.owner.name, } ) return JsonResponse({"MESSAGE": result}, status=200)
โ๏ธ ์ด์ ์๋์ ๊ฐ์ด Httpi ์์ฒญ์ ๋ณด๋ด๋ฉด, DB์์ ๊ฐ์ ๋ถ๋ฌ์ JSONํํ๋ก ๋ฐํํด์ค๋ค.
$ http -v GET 127.0.0.1:8000/owner ๐ OwnerView์ get ๋งค์๋ ์๋
$ http -v GET 127.0.0.1:8000/dog ๐ DogView์ get ๋งค์๋ ์๋
โ๏ธ Comprehension ๋ฌธ๋ฒ์ List, Dict, Tulple ๋ฑ ๋ชจ๋ ์ฌ์ฉํ ์ ์๊ณ , for๋ฌธ์ ํตํด appendํ๋ ๊ฒ๋ณด๋ค ์๋๊ฐ ๋น ๋ฅด๋ค.
import json from django.http import JsonResponse from django.views import View from .models import Owner, Dog # ๐ OwnerView controller class OwnerView(View): def post(self, request): try: data = json.loads(request.body) Owner.objects.create( name=data["name"], email=data["email"], age=data["age"] ) except KeyError: return JsonResponse({"MESSAGE": "IVALID_KEY"}, status=400) return JsonResponse({"MESSAGE": "CREATED"}, status=201) def get(self, request): # ๐ 2์ค List Comprehension result = [ { "name": owner.name, "mail": owner.email, "age": owner.age, "my_dogs": [ {"name": dog.name, "age": dog.age} for dog in owner.dogs.all() # ๐ related_name ์ฌ์ฉ์ผ๋ก "dogs"๋ก ์ญ์ฐธ์กฐ ], } for owner in Owner.objects.all() ] return JsonResponse({"MESSAGE": result}, status=200) # ๐ DogView controller class DogView(View): def post(self, request): try: data = json.loads(request.body) Dog.objects.create( name=data["name"], age=data["age"], owner=Owner.objects.get(name=data["owner"]), ) except KeyError: return JsonResponse({"MESSAGE": "IVALID_KEY"}, status=400) return JsonResponse({"MESSAGE": "CREATED"}, status=201) def get(self, request): # ๐ List Comprehension result = [ { "name": dog.name, "age": dog.age, "owner": dog.owner.name, } for dog in Dog.objects.all() ] return JsonResponse({"MESSAGE": result}, status=200)