django_02_Project,App,Model

Seungju Hwangยท2020๋…„ 9์›” 15์ผ
0

django

๋ชฉ๋ก ๋ณด๊ธฐ
3/11
post-thumbnail

์ฝ”๋“œ

์ˆœ์„œ : ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ -> ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ๋‹ค์šด( django ํ•„์ˆ˜)

-> ํ”„๋กœ์ ํŠธ , ์•ฑ ์ƒ์„ฑ ๋ฐ ์„ค์ • -> model ์ƒ์„ฑ (DB)

โ—พ ํ”„๋กœ์ ํŠธ,์•ฑ ์ƒ์„ฑ ๋ฐ ์„ค์ •

1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

# django-admin startproject [ํ”„๋กœ์ ํŠธ์ด๋ฆ„]
$ django-admin startproject first_project
  • project ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, Python ์ด๋‚˜ Django ์—์„œ ์‚ฌ์šฉ์ค‘์ธ ์ด๋ฆ„์€ ํ”ผํ•ด์•ผ ํ•œ๋‹ค.
  • - ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. (ex. django, test, class, django-test...)

2. ์„œ๋ฒ„ ์‹คํ–‰

$ python manage.py runserver

์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š” ์ง€ ํ™•์ธํ•ด๋ณด์ž! ๋กœ์ผ“๋‚˜์˜ค๋ฉด ํ”Œ์  ์ž˜ ๋งŒ๋“ค์–ด์ง„๊ฒƒ๐Ÿš€. ์ž! ์ด์ œ ์„œ๋ฒ„ ๋„์ž

3. ์•ฑ ์ƒ์„ฑ

# python manage.py startapp [์•ฑ์ด๋ฆ„]
$ python manage.py startapp articles
  • ์ผ๋ฐ˜์ ์œผ๋กœ app ์ด๋ฆ„์€ ๋ณต์ˆ˜ํ˜•์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

4. ์•ฑ ๋“ฑ๋ก

# settings.py

INSTALLED_APPS = [
    'articles',
    ....
]
  • ์ƒ์„ฑํ•œ application์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด djangoํ”„๋กœ์ ํŠธ์— ๋“ฑ๋กํ•ด์•ผ ํ•˜๋ฉฐ, ๋ฐ˜๋“œ์‹œ ์•ฑ์ƒ์„ฑ ํ›„ ๋“ฑ๋กํ•ด์•ผํ•œ๋‹ค.

5. Internationalization ์„ค์ •

# settings.py

LANGUAGE_CODE = 'ko-kr'
TIME_ZONE = 'Asia/Seoul'

์ฐธ๊ณ 

โ—พ Model ์ •์˜ ๋ฐ DB ์—ฐ๋™

1. Model.py ์ •์˜

# articles/models.py
from django.db import models
# ์•„๋ž˜ ๋‘๊ฐœ๋Š” ๋‚œ์ค‘์— (Media - ์ด๋ฏธ์ง€์—…๋กœ๋“œ) ์‚ฌ์šฉํ•  ํŒจํ‚ค์ง€ ์ž…๋‹ˆ๋‹ค.
from imagekit.models import ProcessedImageField
from imagekit.processors import Thumbnail

Class Article(models.Model):
	title = models.CharField(max_lenght=10)
    content = models.TextField()
    created_at = models.DataTimeField(auto_now_add = True)
    updated_at = models.DataTimeField(auto_add = True)
    # ์ด๋ฏธ์ง€๋ฅผ ๋‹ด์•„๋‘˜ ํ•„๋“œ
    image = ProcessedImageField(
        blank=True,
        format="JPEG",
        processors=[Thumbnail(200,300)],
        upload_to="%Y/%m/%d/"
    )

2. Migration ์ž‘์—…

django๊ฐ€ ๋ชจ๋ธ์— ์ƒ๊ธด ๋ณ€ํ™”(ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค๋˜๊ฐ€ ๋ชจ๋ธ์„ ์‚ญ์ œํ–ˆ๋‹ค๋˜๊ฐ€ ๋“ฑ)๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

makemigrations

$ python manage.py makemigrations
  • migraionํŒŒ์ผ์€ DB ์Šคํ‚ค๋งˆ๋ฅผ ์œ„ํ•œ ๋ฒ„์ „๊ด€๋ฆฌ ์‹œ์Šคํ…œ

-> articles/migrations/ 0001_initial.py ์ƒ์„ฑ

migrate

$ python manage.py migrate
  • ์„ค๊ณ„๋„๋ฅผ ์‹ค์ œ DB์— ๋ฐ˜์˜ํ•˜๋Š” ๊ณผ์ •

--์˜ต์…˜--

sqlmigrate

$ python manage.py sqlmigrate aricles 0001
  • ํ•ด๋‹น migrations ์„ค๊ณ„๋„๊ฐ€ SQL ๋ฌธ์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์„๋˜์–ด์„œ ๋™์ž‘ํ• ์ง€ ๋ฏธ๋ฆฌ ํ™•์ธ

showmigrations

$ python manage.py showmigrations
  • migrations ์„ค๊ณ„๋„๋“ค์ด migrate ๋๋Š”์ง€ ์•ˆ๋๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ

0. VSCode extension

sqlite ์„ค์น˜ : vscode ๋‚ด์—์„œ db๊ตฌ์„ฑ์„ ์‰ฝ๊ฒŒ ๋ณด์—ฌ์ฃผ๋Š” ํ™•์žฅํŒŒ์ผ

--------------------------------

๊ฐœ๋…

โ—ฝ Model.py

1. ๋Œ€ํ‘œ ํ•„๋“œ

  • CharField(max_length=None, **options)

    • ๊ธธ์ด์˜ ์ œํ•œ์ด ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋„ฃ์„ ๋•Œ ์‚ฌ์šฉ
    • CharField์˜ max_length๋Š” ํ•„์ˆ˜ ์ธ์ž
    • ํ•„๋“œ์˜ ์ตœ๋Œ€ ๊ธธ์ด(๋ฌธ์ž), ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ๊ณผ Django์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(๊ฐ’์„ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ)์—์„œ ํ™œ์šฉ
    • ๋ฌธ์ž์—ด ํ•„๋“œ โ†’ ํ…์ŠคํŠธ ์–‘์ด ๋งŽ์„ ๊ฒฝ์šฐ TextField() ์‚ฌ์šฉ
    • ๊ธฐ๋ณธ ์–‘์‹ ์œ„์ ฏ์€ TextInput
  • TextField(**options)

    • ๊ธ€์˜ ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ ์‚ฌ์šฉ
    • max_length ์˜ต์…˜์„ ์ฃผ๋ฉด ์ž๋™์–‘์‹ํ•„๋“œ์˜ textarea ์œ„์ ฏ์— ๋ฐ˜์˜์€ ๋˜์ง€๋งŒ ๋ชจ๋ธ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ˆ˜์ค€์—๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. (CharField ๋ฅผ ์‚ฌ์šฉ)
    • ๊ธฐ๋ณธ ์–‘์‹ ์œ„์ ฏ์€ Textarea
  • DateTimeField(auto_now=False, auto_now_add=False, **options)

    • ์ตœ์ดˆ ์ƒ์„ฑ ์ผ์ž

      • auto_now_add=True

      • django ORM์ด ์ตœ์ดˆ insert(ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ž…๋ ฅ)์‹œ์—๋งŒ ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์œผ๋กœ ๊ฐฑ์‹ (ํ…Œ์ด๋ธ”์— ์–ด๋–ค ๊ฐ’์„ ์ตœ์ดˆ๋กœ ๋„ฃ์„ ๋•Œ)

    • ์ตœ์ข… ์ˆ˜์ • ์ผ์ž

      • auto_now=True

      • django ORM์ด save๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์œผ๋กœ ๊ฐฑ์‹ 

โ—ฝ Migrations

1. makemigrations

  • ๋ชจ๋ธ์„ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์— ๊ธฐ๋ฐ˜ํ•œ ์ƒˆ๋กœ์šด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ
  • ๋ชจ๋ธ์„ ํ™œ์„ฑํ™” ํ•˜๊ธฐ ์ „์— DB ์„ค๊ณ„๋„(๋งˆ์ด๊ทธ๋ ˆ์ด์…˜) ์ž‘์„ฑ
  • migrations ํด๋” ์•ˆ์— ์ •์˜ํ•œ class๋ฅผ ํ† ๋Œ€๋กœ Django ORM์ด ์šฐ๋ฆฌ์—๊ฒŒ ๋งŒ๋“ค์–ด์ค€ ์„ค๊ณ„๋„ ํ™•์ธ

2. migrate

  • ๊ธฐ์กด์— ๋‚ด๊ฐ€ [models.py](<http://models.py>) ์— ์ •์˜ํ•˜์ง€ ์•Š์•˜๋˜ ํ…Œ์ด๋ธ”์€ ์žฅ๊ณ ๊ฐ€ ๊ธฐ์กด์— ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ ํ…Œ์ด๋ธ”

  • migrate ๋Š” makemigrations ๋กœ ๋งŒ๋“  ์„ค๊ณ„๋„๋ฅผ ์‹ค์ œ db.sqlite3 DB์— ๋ฐ˜์˜ํ•œ๋‹ค.

  • ๋ชจ๋ธ์—์„œ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ๋“ค๊ณผ DB์˜ ์Šคํ‚ค๋งˆ๊ฐ€ ๋™๊ธฐํ™”๋ฅผ ์ด๋ฃฌ๋‹ค.

โ—ฝ Database API

django๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ orm์„ ์ œ๊ณตํ•จ์— ๋”ฐ๋ฅธ ๊ฒƒ์œผ๋กœ db๋ฅผ ํŽธํ•˜๊ฒŒ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ

๊ตฌ๋ฌธ : ์ฐธ๊ณ 1 ์ฐธ๊ณ 2

1. Objects manager

Django ๋ชจ๋ธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ ์ž‘์—…์ด ์ œ๊ณต๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์ฐธ๊ณ 

  • Model Manager์™€ Django Model ์‚ฌ์ด์˜ *Query ์—ฐ์‚ฐ์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• * ์„ ํ•ด์ฃผ๋Š” ์นœ๊ตฌ
  • ์ฆ‰, models.py ์— ์„ค์ •ํ•œ ํด๋ž˜์Šค(ํ…Œ์ด๋ธ”)์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ๋•Œ DB์™€์˜ interface ์—ญํ• ์„ ํ•˜๋Š” ๋งค๋‹ˆ์ €
  • ORM์ด ํด๋ž˜์Šค๋กœ ๋งŒ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์™€ db๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š”๋ฐ ๊ทธ ์‚ฌ์ด์—์„œ ํ†ต์—ญ ์—ญํ• ์„ ํ•˜๋Š”๊ฒŒ ORM์˜ ์—ญํ• ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์ฆ‰, DB๋ฅผ Python class๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” manager
    • Python Class(models.py) โ€”โ€”โ€” objects โ€”โ€”โ€” DB
  • Django๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  Django ๋ชจ๋ธ ํด๋ž˜์Šค์— ๋Œ€ํ•ด 'objects' ๋ผ๋Š” Manager(django.db.models.Manager) ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • Manager(objects)๋ฅผ ํ†ตํ•ด ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘(๋ฉ”์„œ๋“œ)ํ•  ์ˆ˜ ์žˆ๋‹ค.

2. QuerySet API

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ํ•„ํ„ฐ๋ฅผ ๊ฑธ๊ฑฐ๋‚˜ ์ •๋ ฌ ๋“ฑ์„ ์ˆ˜ํ–‰

์ฟผ๋ฆฌ(์งˆ๋ฌธ)๋ฅผ DB์—๊ฒŒ ๋˜์ ธ์„œ ๊ธ€์„ ์ฝ๊ฑฐ๋‚˜, ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ์‚ญ์ œ

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ „๋‹ฌ ๋ฐ›์€ ๊ฐ์ฒด์˜ ๋ชฉ๋ก
  • django orm์—์„œ ๋ฐœ์ƒํ•œ ์ž๋ฃŒํ˜•
  • objects๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด
  • ๋‹จ์ผํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•  ๋•Œ๋Š” ํ…Œ์ด๋ธ”(Class)์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋ฆฌํ„ด๋จ
profile
๊ธฐ๋กํ•˜๋Š” ์Šต๊ด€์€ ์‰ฝ๊ฒŒ ๋ฌด๋„ˆ์ง€์ง€ ์•Š์•„์š”.

0๊ฐœ์˜ ๋Œ“๊ธ€