MVT๋?
Model: ๋ฐ์ดํฐ ๊ตฌ์กฐํ
View: ๋ฐ์ดํฐ๊ฐ ํ๋ฌ๋ค๋๋ ๊ณณ
Template: ๋ฐ์ดํฐ๋ฅผ ํ์ํ๋ ๊ณณ
API ๋?
Application Programming Interface
๊ฐ๋ฐ์์ฉ ์ ์
๊ฐ๋ฐ์๋ Data ๋ง ํ์ํจ!
Request
: ์์ฒญ์ URL๋ก ๋ณด๋ธ๋ค!
Data์ ํ๊ธฐ๋ฒ
: ์ฝ์
JSON
JavaScript Object Notation
Javascript ๊ฐ์ฒด์ ํ๊ธฐ๋ฒ
XML
eXtended Markup Language (W3C, 1996)
Why not HTML?
: key๊ฐ์ด ๋ฐ์์ด ์ ๋จ!
๊ทธ๋์ ๋ฑ์ฅํ ๊ฒ์ด tag
๋ฅผ ๋ด๋ง๋๋ก ์ ์ํ ์ ์๋ XML
Why JSON
XML์ด ๋ซ๋ tag ๋๋ฌธ์ ๊ธธ์ด๊ฐ ๊ธธ๋ค
์ฐ๋ฆฌ๊ฐ ํ ์ผ
: Django ์์ JSON ํ์์ ๋ง์ถฐ์ Data๋ง ์ ๊ณตํ๋ค!
JSON ... ๊ทธ๋ฆฌ๊ณ ๋์๋?
Javscript & framewokr๋ฅผ ๋ถ๋ฆฌํ๋ ์ด์
์ข์ ์ ์ ๊ฒฝํ์ ์ํด์
UX ์ ์ข์ผ๋ฉด -> User X -> ๋ X
data -> ์ธ๊ฐ์ด ๋ญ ์ข์ํ ๊น?
๋ชจ๋ฐ์ผ ์ดํ๋ฆฌ์ผ์ด์
(์น)
๋ถ๋ฆฌ๋์ด ์๋ ๊ฒ์ด ํธํด์
Django ๋ค์ ๊น๊ธฐ
Copy $ pip uninstall django
$ pip install django==2.1.15
Faker ์ฌ์ฉํ๊ธฐ
faker ์ค์น
Dummy data ๋ง๋ค๊ธฐ
Copy In [1]: from faker import Faker
In [2]: f = Faker()
In [3]: f.text()
Out[3]: 'Soldier live various argue many expect important once. Next possible whom I.\nSome national left wall score few else always. Action less culture spring any night.'
In [4]: f.name()
Out[4]: 'Jenna Davis'
In [5]: f.paragraph()
Out[5]: 'Improve knowledge hot matter himself. Growth water act bill to can discuss there. Follow out person vote action someone.'
In [6]: f.paragraph(4)
Out[6]: 'Early program four bill. Comput
RESTful API
: url์ ๊น๋ํ๊ฒ ์ ๋ฆฌํ๋ ๋ฐฉ์ (๊ณตํต์ rule / ์ฝ์)
RESTful
๋ช
์ฌ (๋ณต์ํ)๋ก ๊ตฌ์
๊ท์น๋ค
๋์ฌ URL์ ์ง์ด ๋ฃ์ง๋ง! -> HTTP method
ํ์ฉํด
R (GET)
index (๋ชจ๋ ์ ๋ณด) - (GET) / articles /
detail (ํ๋์ ์ ๋ณด) - (GET) / articles / <id>
D (DELETE)
(DELETE) / articles / <id>
๋ชฉ์ ์ด๋ง URL์ ์ง์ด ๋ฃ์ด -> ๋ณต์ํ์ผ๋ก
API ๊ด๋ จ URL
versionning
ssafy.com/api/v1/lectures/
POST /api/articles/1/like/
POST /api/articles/1/comments/like/
Django REST Framework (DRF)
djangorestframework ์ค์น
Copy $ pip install djangorestframework
์ค์น๋์ด ์๋์ง ํ์ธ
Copy $ pip show djangorestframework
Name: djangorestframework
Version: 3.11.0
Summary: Web APIs for Django, made easy.
Home-page: https://www.django-rest-framework.org/
Author: Tom Christie
Author-email: tom@tomchristie.com
License: BSD
Location: /home/chloe/.local/lib/python3.6/site-packages
Requires: django
Required-by: drf-serializer-cache
Serialize (์ง๋ ฌํ)
ํฌ๋งท์ ๋ณํ (๋ฐ์ดํฐ๋ฅผ ์ ์ก/์ด๋)
dict -> JSON (stringify , serialize
)
JSON -> dict (parse , deserialize
)
์ง๋ ฌํ
: Object(์ธ์ด, database) -> String (JSON)
CREATE
raise_exception
์ผ๋ก Error ์์๊ฒ ์ถ๋ ฅํ๊ธฐ
ex)
Copy @api_view(['POST'])
def article_create(request):
# ๊ธ์ ์์ฑ
serializer = ArticleSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data)
ํ๋๋ง ๋ณด๋
์๋ฌ๋ฉ์์ง
yasg
API ๊ด๋ จ ๋ฌธ์๋ฅผ ์๋์ผ๋ก ์์ฑ
DRF yasg ์ค์นํ๊ธฐ
Copy $ pip install drf-yasg
Dummy data JSON ์ผ๋ก ๋ถ๋ฌ์ค๊ธฐ
fixtures ํด๋์ dummy.json
๋ฃ๊ธฐ
Copy โโโ api
โ โโโ __init__.py
โ โโโ settings.py
โ โโโ urls.py
โ โโโ wsgi.py
โโโ db.sqlite3
โโโ manage.py
โโโ musics
โโโ admin.py
โโโ apps.py
โโโ fixtures
โ โโโ dummy.json
โโโ __init__.py
โโโ migrations
โโโ models.py
โโโ serializers.py
โโโ tests.py
โโโ urls.py
โโโ views.py
7 directories, 29 files
loaddata
๋ก dummy.json ์ DB์ ๋ฃ๊ธฐ
Copy $ python manage.py loaddata dummy.json
Installed 14 object(s) from 1 fixture(s)
dumpdata
๋ก DB์ ์๋ data dumping ํ๊ธฐ
Copy $ python manage.py dumpdata musics
[{"model": "musics.artist", "pk": 1, "fields": {"name": "Coldplay"}}, {"model": "musics.artist", "pk": 2, "fields": {"name": "Maroon5"}}, {"model": "musics.music", "pk": 1, "fields": {"artist": 2, "title": "Girls Like You"}}, {"model": "musics.music", "pk": 2, "fields": {"artist": 2, "title": "Sunday Morning"}}, {"model": "musics.music", "pk": 3, "fields": {"artist": 1, "title": "viva la vida"}}, {"model": "musics.music", "pk": 4, "fields": {"artist": 1, "title": "paradise"}}, {"model": "musics.comment", "pk": 1, "fields": {"music": 1, "content": "\uac78\uc2a4 \ub77c\uc78c \uc720!!!"}}, {"model": "musics.comment", "pk": 2, "fields": {"music": 1, "content": "\ub9c8\ub8ec \ud30c\uc774\ube0c \uc9f1\uc9f1!"}}, {"model": "musics.comment", "pk": 3, "fields": {"music": 2, "content": "\uc77c\uc694\uc77c \ubaa8\ub2dd~~~"}}, {"model": "musics.comment", "pk": 4, "fields": {"music": 2, "content": "\ud558\uc9c0\ub9cc \ub0b4\uc77c\uc740 \uc6d4\uc694\uc77c"}}, {"model": "musics.comment", "pk": 5, "fields": {"music": 3, "content": "10\ub144\uc774 \uc9c0\ub098\ub3c4 \uc88b\uc544"}}, {"model": "musics.comment", "pk": 6, "fields": {"music": 3, "content": "\ub9c8\uce58 \ub0b4\uac00 \uc655\uc774 \ub41c \uac83 \uac19\uc544!"}}, {"model": "musics.comment", "pk": 7, "fields": {"music": 4, "content": "\ud30c\ub77c\ub2e4\uc774\uc2a4 \ud30c\ub77c\ud30c\ub77c\ud30c\ub77c\ub2e4\uc774\uc2a4~"}}, {"model": "musics.comment", "pk": 8, "fields": {"music": 4, "content": "\uc228\uaca8\uc9c4 \uba85\uace1!!!"}}]
dumpdata
๋ก dumping ํ data๋ฅผ JSON file๋ก ๋ง๋ค๊ธฐ
์ด๋ ๊ฒ ํ๋ฉด ๋ค๋ฅ๋ค๋ฅ ๋ถ์ด์์
Copy $ python manage.py dumpdata musics > dump.json
indenting ์ค์ ์์๊ฒ ๋ง๋ค๊ธฐ
--indent 2
-> indenting์ 2 ์ค๋ผ
Copy $ python manage.py dumpdata musics --indent 2 > dump2.json
result
Copy [
{
"model": "musics.artist",
"pk": 1,
"fields": {
"name": "Coldplay"
}
},
{
"model": "musics.artist",
"pk": 2,
"fields": {
"name": "Maroon5"
}
},
{
"model": "musics.music",
"pk": 1,
"fields": {
"artist": 2,
"title": "Girls Like You"
}
},
{
"model": "musics.music",
"pk": 2,
"fields": {
"artist": 2,
"title": "Sunday Morning"
}
},
{
"model": "musics.music",
"pk": 3,
"fields": {
"artist": 1,
"title": "viva la vida"
}
},
{
"model": "musics.music",
"pk": 4,
"fields": {
"artist": 1,
"title": "paradise"
}
},
{
"model": "musics.comment",
"pk": 1,
"fields": {
"music": 1,
"content": "\uac78\uc2a4 \ub77c\uc78c \uc720!!!"
}
},
{
"model": "musics.comment",
"pk": 2,
"fields": {
"music": 1,
"content": "\ub9c8\ub8ec \ud30c\uc774\ube0c \uc9f1\uc9f1!"
}
},
{
"model": "musics.comment",
"pk": 3,
"fields": {
"music": 2,
"content": "\uc77c\uc694\uc77c \ubaa8\ub2dd~~~"
}
},
{
"model": "musics.comment",
"pk": 4,
"fields": {
"music": 2,
"content": "\ud558\uc9c0\ub9cc \ub0b4\uc77c\uc740 \uc6d4\uc694\uc77c"
}
},
{
"model": "musics.comment",
"pk": 5,
"fields": {
"music": 3,
"content": "10\ub144\uc774 \uc9c0\ub098\ub3c4 \uc88b\uc544"
}
},
{
"model": "musics.comment",
"pk": 6,
"fields": {
"music": 3,
"content": "\ub9c8\uce58 \ub0b4\uac00 \uc655\uc774 \ub41c \uac83 \uac19\uc544!"
}
},
{
"model": "musics.comment",
"pk": 7,
"fields": {
"music": 4,
"content": "\ud30c\ub77c\ub2e4\uc774\uc2a4 \ud30c\ub77c\ud30c\ub77c\ud30c\ub77c\ub2e4\uc774\uc2a4~"
}
},
{
"model": "musics.comment",
"pk": 8,
"fields": {
"music": 4,
"content": "\uc228\uaca8\uc9c4 \uba85\uace1!!!"
}
}
]