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
https://meetup.toast.com/posts/92 μ°Έκ³ νκΈ°
: 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 μ€μΉνκΈ°
https://drf-yasg.readthedocs.io/en/stable/readme.html
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!!!"
}
}
]