Django-base.md


  • Django framework๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•œ ์žก๋‹คํ•œ ๊ฐœ๋…๋“ค์„ ์ ์€ ๊ณณ

django ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ์ ์ธ ์ƒ์„ฑ ๋ฐ ์„ค์ •

# ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑํ•˜๊ธฐ
$ python3 -m venv [๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„] #python ๋‚ด์žฅ ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ
# in UNIX
$ source [๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„]/bin/activate
# in CMD
$ [๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„]\Scripts\activate

# django ์„ค์น˜
(venv) $ pip install django black # black: python ์ž๋™ ํฌ๋งทํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

# django ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
(venv) $ django-admin startproject [ํ”„๋กœ์ ํŠธ ์ด๋ฆ„]
(venv) $ cd [ํ”„๋กœ์ ํŠธ ์ด๋ฆ„]

# django ์•ฑ ์ƒ์„ฑ
(venv) $ python manage.py startapp [์•ฑ ์ด๋ฆ„]

# django ํ”„๋กœ์ ํŠธ ์‹คํ–‰
(venv) $ python manage.py runserver
# ํฌํŠธ๋ฅผ ๋ฐ”๊พธ์–ด ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์„ ๋•Œ
(venv) $ python manage.py runserver [๋ฐ”๊ฟ€ ํฌํŠธ]
# localhost๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ IP๋กœ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ
(venv) $ python manage.py runserver [IP]:[๋ฐ”๊ฟ€ ํฌํŠธ]

# admin ํŽ˜์ด์ง€ ์‚ฌ์šฉ์„ ์œ„ํ•ด superuser ์ƒ์„ฑ
(venv) $ python manage.py createsuperuser
... ๋‚˜์˜ค๋Š” ์ •๋ณด๋“ค ์ž…๋ ฅ...
Superuser created successfully.
  • ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ root directroy๋Š” ํ”„๋กœ์ ํŠธ์˜ ์ปจํ…Œ์ด๋„ˆ์ผ ๋ฟ์ด๋ผ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”๋„ ์ƒ๊ด€์—†๋‹ค.

  • manage.py : django ํ”„๋กœ์ ํŠธ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  command-line utility

  • ์•ˆ์ชฝ์˜ ํ”„๋กœ์ ํŠธ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ default app ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์‹ค์งˆ์ ์ธ ํ”„๋กœ์ ํŠธ์˜ ํŒจํ‚ค์ง€์ด๋‹ค.

  • runserver๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด django์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ localhost:8000์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

django model queryset


django๋Š” ORM์„ ์ œ๊ณตํ•œ๋‹ค. ORM์„ ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€ ORM์ด ์ฟผ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • django์˜ ์ฟผ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ง€์—ฐ๋œ๋‹ค. django์˜ queryset์€ DB์˜ ์—ฌ๋Ÿฌ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ํ•„ํ„ฐ๋ง์ด ๊ฐ€๋Šฅํ•˜๋‹ค

person_set = Person.objects.filter(name="Tom")

์ด ์ฝ”๋“œ๋Š” DB์— ์–ด๋–ค ์ฟผ๋ฆฌ๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” DB์— ์•„๋ฌด๋Ÿฐ ๋ฉ”์‹œ์ง€๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์—์„œ ์žฅ์ ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด DB์— ์ฟผ๋ฆฌ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ผ์ด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋Š๋ ค์ง€๊ฒŒ ํ•˜๋Š” ์ฃผ๋ฒ” ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • django์˜ ์ฟผ๋ฆฌ์…‹์€ ์บ์‹œ๋œ๋‹ค. django queryset์„ ์ˆœํšŒํ•˜๋Š” ์‹œ์ ์—์„œ DB์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‹ค์ œ๋กœ ๊ฐ€์ ธ์˜ค๊ณ , ์ด๋Š” ๋ชจ๋‘ django ๋ชจ๋ธ๋กœ ๋ณ€ํ™˜๋˜์–ด ์บ์‹œ๋œ๋‹ค(ํ‰๊ฐ€). ๋”ฐ๋ผ์„œ queryset์„ ๋‹ค์‹œ ์ˆœํšŒํ•˜๋”๋ผ๋„ ๋˜‘๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ DB์— ์—ฌ๋Ÿฌ๋ฒˆ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • if๋ฌธ์€ ์ฟผ๋ฆฌ์…‹์„ ํ‰๊ฐ€ํ•œ๋‹ค. ์บ์‹œ ๊ธฐ๋Šฅ์€ queryset์— ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•œ ํ›„ ํ•˜๋‚˜๋ผ๋„ ๋ฐœ๊ฒฌ๋˜์—ˆ์„ ๋•Œ๋งŒ ์ˆœํšŒํ•˜๋ ค ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

person_set = Person.objects.filter(gender='male')
# if ๋ฌธ์€ queryset์„ 'ํ‰๊ฐ€'ํ•œ๋‹ค.
if person_set:
    for person in persion_set:
        print(person.name)

ํ•˜์ง€๋งŒ, ๊ฒฐ๊ณผ ์ „์ฒด๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ง€ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ตœ์†Œํ•œ ํ•˜๋‚˜๋Š” ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์—๋Š” if ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด queryset์ด ํ‰๊ฐ€๋˜๊ณ  queryset ์บ์‹œ์— ์ „์ฒด ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ €์žฅ๋˜๋Š” ๋น„ํšจ์œจ์ ์ธ ์ž‘์—…์ด ์ผ์–ด๋‚˜๊ฒŒ๋œ๋‹ค. ์ด๋Ÿด๋–„๋Š” exists() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ž

person_set = Person.objects.filter(name='Tom')
# if ๋ฌธ์€ queryset์„ 'ํ‰๊ฐ€'ํ•œ๋‹ค.
if person_set.exists():
    # DB์—์„œ ๊ฐ€์ ธ์˜จ ๋ ˆ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ํŠธ๋ž˜ํ”ฝ๊ณผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.
    print('Tom exists')

django-rest-framework


  • django๋ฅผ ์‚ฌ์šฉํ•ด web API๋ฅผ ๊ฐœ๋ฐœ ํ• ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” serializer, permisison, authentication, view ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • Installation

$ pip install djangorestframework
  • in INSTALLED_APPS

'rest_framework' ์ถ”๊ฐ€

Last updated