Django-model.md


Python์˜ Web framework์ธ Django์—์„œ๋Š” ORM์„ ๊ธฐ๋ณธ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด app ๋””๋ ‰ํ† ๋ฆฌ์˜ models.py์— class ํ˜•ํƒœ๋กœ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

model ์ •์˜


  • models.py

from django.db import Model

class User(Model.models):
    username = models.CharField(max_length=100)
    email = models.EmailField()
    pwd = models.CharField(max_length=200)
    
  • django.db๋ชจ๋“ˆ์˜ Model.models๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ธฐ๋ณธ ๋ชจ๋ธ์˜ ํ‹€์„ ๋งŒ๋“ ๋‹ค.

  • class์˜ ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์–ด์งˆ Table์˜ ์ด๋ฆ„์„ ๋ช…์‹œํ•œ๋‹ค.

  • django ORM๊ณผ ์—ฐ๊ฒฐ๋  DB๋Š” settings.py์—์„œ ์„ค์ •ํ•œ๋‹ค.

django model์„ ๋งŒ๋“ค ๋•Œ primary_key๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด id๋ผ๋Š” ์ด๋ฆ„์˜ AutoField ํ•„๋“œ๊ฐ€ primary_key๊ฐ€ ๋˜์–ด ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

Migration

  • migrate -> ์ด์ฃผํ•˜๋‹ค

  • ์ฆ‰, django ORM์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ด์ฃผ ์‹œํ‚ค๋Š” ๊ฒƒ

  • make migrations : ์ฝ”๋“œ๋กœ ์ž‘์„ฑ๋œ ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๊ณ  ์•„์ง ์กด์žฌํ•˜์ง€ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์ƒ์„ฑ

# ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ƒ์„ฑ
$ python manage.py makemigrations <app-name>
# ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ ์šฉ
$ python manage.py migrate <app-name>

Field types


  • auto_now: ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋ ๋•Œ(Model.save())์˜ ๋‚ ์งœ๋ฅผ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

  • auto_now_add: ๊ฐ์ฒด๊ฐ€ ์ตœ์ดˆ ์ €์žฅ(insert)์‹œ์˜ ๋‚ ์งœ๋ฅผ ์ž๋™์œผ๋กœ ์ ์šฉํ•œ๋‹ค.

  • CharField์ด์ง€๋งŒ EmailValidator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฉ”์ผ์˜ ํ˜•์‹์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

  • FileField์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๊ณ , height์™€ width ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค.

  • Integerfield๋ฅผ ์ƒ์†๋ฐ›๊ณ  auto_increment๋ฅผ ์ ์šฉํ•˜๋Š” ํ•„๋“œํƒ€์ž…

Field name restrictions

  • Python ๋‚ด์žฅ ์˜ˆ์•ฝ์–ด ์‚ฌ์šฉ X

  • underscore(_) ๋‘๊ฐœ ์ด์ƒ ์‚ฌ์šฉ X

pass = models.BooleanField() # => X
foo__bar = models.IntegerField() # => X

Foreign Key


  • 1 : N ๊ด€๊ณ„

  • fk name์€ ๋ณดํ†ต ์ฐธ์กฐ๋˜๋Š” Table ๋ช…์— _id๊ฐ€ ๋ถ™์–ด ๋งŒ๋“ค์–ด์ง„๋‹ค.

  • ForeignKey์˜ ๊ฐ’์€ NULL์ด๊ฑฐ๋‚˜ ๋ถ€๋ชจํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค ๊ฐ’๊ณผ ๋™์ผํ•ด์•ผ ํ•จ

# ์ง์ ‘ class ๋งคํ•‘ํ•ด์ฃผ๊ธฐ
from account.models import User

class Post(Model.models):
    email = models.ForeignKey(User)

# 'appLabel.๋ชจ๋ธ์ด๋ฆ„' ํ˜•์‹์œผ๋กœ ๋ช…์‹œ
class Post(Model.models):
    email = models.ForeignKey('account.User')
  • ์œ„์™€ ๊ฐ™์ด 2๊ฐ€์ง€์˜ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์•ฑ๋ผ๋ฆฌ ๋ชจ๋ธ๋ช…์ด ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์˜ ํ˜•์‹์„ ์„ ํ˜ธํ•œ๋‹ค.

  • on_delete

    • ์ฐธ์กฐ๋ฌด๊ฒฐ์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ์œ„ํ•ด ForeignKey๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋  ๋•Œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์˜ต์…˜์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

    • CASCADE

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋  ๋•Œ ํ•ด๋‹น ForeignKeyField๋ฅผ ํฌํ•จํ•˜๋Š” ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค(row)๋„ ์‚ญ์ œํ•œ๋‹ค.

    • PROTECT

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋ ๋•Œ ์‚ญ์ œ๋˜์ง€ ์•Š๋„๋ก ProtectedError๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

    • SET_NULL

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋ ๋•Œ ํ•ด๋‹น ForeignKeyField์˜ ๊ฐ’์„ null๋กœ ๋ฐ”๊พผ๋‹ค. (๋‹จ, null=True์ผ ๋•Œ๋งŒ ๊ฐ€๋Šฅ)

    • SET_DEFAULT

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋  ๋•Œ ForeignKeyField๊ฐ’์„ default๊ฐ’์œผ๋กœ ๋ฐ”๊พผ๋‹ค.(default๊ฐ’์ด ์žˆ์„ ๋•Œ๋งŒ ๊ฐ€๋Šฅ)

    • SET()

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋  ๋•Œ SET์— ์„ค์ •๋œ ํ•จ์ˆ˜ ๊ฐ’์— ์˜ํ•ด ์„ค์ •๋œ๋‹ค.

    • DO_NOTHING

      • ForeignKeyField๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ์‚ญ์ œ๋  ๋•Œ ์•„๋ฌด๋Ÿฐ ํ–‰๋™์„ ์ทจํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฐธ์กฐ๋ฌด๊ฒฐ์„ฑ์„ ํ•ด์น  ์œ„ํ—˜์ด ์žˆ๋‹ค.

๐Ÿšจ ๋ณตํ•ฉํ‚ค (composite key)


  • Django์˜ ์ตœ๋Œ€ ๋‹จ์  ์ค‘ ํ•˜๋‚˜๋‹ค. django์˜ ORM์—์„œ๋Š” ๋ณตํ•ฉํ‚ค๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค!!

์—ฌ๊ธฐ์„œ ๋ณตํ•ฉํ‚ค๋ž€, ํ•˜๋‚˜์˜ ๋ฆด๋ ˆ์ด์…˜์—์„œ ํ•˜๋‚˜์˜ ์†์„ฑ์œผ๋กœ ๊ธฐ๋ณธํ‚ค๊ฐ€ ๋  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋‘˜ ์ด์ƒ์˜ ์ปฌ๋Ÿผ์„ ๋ฌถ์–ด ์‹๋ณ„์ž๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

Last updated