- URLconf, View를 활용하여 리스트를 표출하는 엔드포인트 작성
- 엔드포인트의 결과로 return할 JSON data 생성
- httpie를 이용하여 GET/POST 요청보내기
$ pip install httpie
사용예시
$ http -v POST 127.0.0.1:8000/owners name='홍길동' email='abc123@naver.com' age=25
$ http -v GET 127.0.0.1:8000/owners
models.py 에 Model Class 작성을 통해서 Database의 테이블과 Mapping
owners/models.py
from django.db import models
#Owner Class
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 Class
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'
models.py 작성한 내용 DB에 적용
# 1. makemigrations : migration 파일 생성(설계도 생성)
$ python manage.py makemigrations
# 2. migrate : 생성한 migration파일을 database에 적용
$ python manage.py migrate
def post(self, request):
"""
목적: Client가 보내주는 owner정보(name, age, email) 등록 요청에 맞게 owner테이블 in database 에 저장
Input:
{
'name' = '홍길동'
'age' = 20
'email' = asd@gmail.com
}
Logic:
1. owner 모델을 사용해서 owner 테이블에 데이터 입력
2. Owner.objects.create(...)
Output:
{
"message": "SUCCESS"
}, status = 201
"""
return HttpResponse(201)
def get(self, request):
"""
목적: owners의 정보를 client 요청하는 바와 맞게 database에서 호출하고, 가공해서 응답(반환)
Logic:
1. 응답해야할 방식에 맞춰주기 위해서 빈 리스트 생성 / results = []
2. 모든 owner의 정보 추출 / owners = Owner.Objects.all()
3. for loop으로 강아지 리스트(name , age 포함되도록)가 함께 응답될 수 있도록 짜야함
Output:
{
"results" = result
}, status = 200
"""
return HttpResponse(200)
import json
from django.http import JsonResponse
from django.views import View
from owners.models import Owner, Dog
class OwnersView(View):
def post(self, request):
data = json.loads(request.body)
Owner.objects.create(
name = data['name'],
age = data['age'],
email = data['email']
)
return JsonResponse({"message" : "SUCCESS"}, status = 201)
def get(self, request):
for owner in owners:
dogs = list(Dog.objects.filter(owner=owner.id).values('name', 'age'))
results.append(
{
'name' : owner.name,
'age' : owner.age,
'email': owner.email,
'dogs' : dogs,
}
)
return JsonResponse({'results' : results}, status = 200)
dogs = list(Dog.objects.filter(owner=owner.id).values('name', 'age'))
results = []
owners = Owner.objects.
for owner in owners:
results.append(
{
'name' : owner.name,
'age' : owner.age,
'email': owner.email,
'dogs' : [{
'name': dog.name,
'age' : dog.age,
} for dog in owner.dog_set.all()]
}
)
results = []
▶︎ List Comprehension) results = [
{
'name' : owner.name,
'age' : owner.age,
'email': owner.email,
'dogs' : [{
'name': dog.name,
'age' : dog.age,
} for dog in owner.dog_set.all()]
} for owner in Owner.objects.all()
]
pip install ipython
▻ shell을 보기 편하게 만들어줌pip install django-extensions
▻ shell_plus를 사용 가능하게 해줌(기타 Django Extensions)INSTALLED_APPS
에 django_extensions
를 추가해야함python manage.py shell_plus
▻ shell_plus를 사용해서 더 좋은 shell을 사용 가능!!!!!!def post(self, request):
"""
목적: Client가 보내주는 dog정보(name, age, owner_id) 등록 요청에 맞게 dog테이블 in database 에 저장
Input:
{
'name' = '라온'
'age' = 1
'owner' = '홍길동'
}
Logic:
1. dog 모델을 사용해서 dog 테이블에 데이터 입력
2. Dog.objects.create()
Output:
{
"message": "SUCCESS"
}, status = 201
"""
return HttpResponse(201)
def get(self, request):
"""
목적: dogs의 정보를 client 요청하는 바와 맞게 database에서 호출하고, 가공해서 응답(반환)
Logic:
1. 응답해야할 방식에 맞춰주기 위해서 빈 리스트 생성 / results = []
2. 모든 dog의 정보 추출 / dogs = Dog.Objects.all()
Output:
{
"results" = result
}, status = 200
"""
return HttpResponse(200)
List Comprehension
으로 줄여줄 수 있음)class DogsView(View):
def post(self, request):
data = json.loads(request.body)
owner = Owner.objects.get(name=data['owner'])
Dog.objects.create(
name = data['name'],
age = data['age'],
owner_id = owner.id
)
return JsonResponse({"message" : "SUCCESS"}, status = 201)
def get(self, request):
results = []
dogs = Dog.objects.all()
for dog in dogs:
results.append(
{
'name' : dog.name,
'age' : dog.age,
'owner': dog.owner.name
}
)
return JsonResponse({"results" : results}, status=200)
from django.urls import path, include
urlpatterns = [
# owners
path('', include('owners.urls'))
]
$ touch urls.py
명령어로 생성from django.urls import path
from owners.views import OwnersView, DogsView
urlpatterns = [
path('owners', OwnersView.as_view()),
path('dogs', DogsView.as_view())
]
$ python manage.py runserver 0:8000
로 서버 실행(오류없는지 확인)'''
Owners
http -v POST http://localhost:8000/owners name='홍길동' email='qwert@gmail.com' age=21
http -v GET http://localhost:8000/owners
Dogs
http -v POST http://localhost:8000/dogs name='라온' age=1 owner='홍길동'
http -v GET http://localhost:8000/dogs
'''