django 02-1 Model(orm)

Seungju Hwangยท2020๋…„ 10์›” 4์ผ
0

django

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

ORM

์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ORM ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ๋””ํ…Œ์ผํ•˜๊ฒŒ ์•Œ์•„๋ด…์‹œ๋‹ค

ORM์„ ์“ฐ๋Š” ์ด์œ ๋Š” DB์กฐ์ž‘์„ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(ํด๋ž˜์Šค)์ฒ˜๋Ÿผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค!!

__str__

๋ชจ๋“  ๋ชจ๋ธ๋งˆ๋‹ค ํ‘œ์ค€ ํŒŒ์ด์ฌ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์ธ str() ์„ ์ •์˜ํ•˜์—ฌ ๊ฐ๊ฐ์˜ object๊ฐ€ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜(return)ํ•˜๋„๋ก ํ•œ๋‹ค.

class Article(models.Model):
    title = models.CharField(max_length=10)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

โ—พ Create

๊ธฐ์ดˆ์„ค์ •

  • ์ •์˜ํ•œ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, importํ•ด์„œ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
from .models import Article

Article.objects.all()
>>> <QuerySet []>

๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” 3๊ฐ€์ง€ ๋ฐฉ์‹

  • ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
    • article = Article() : ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    • article.title : ์ธ์Šคํ„ด์Šค๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•ด ํ•ด๋‹น ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝ
    • article.save() : ์ธ์Šคํ„ด์Šค๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
# Article(class)๋กœ๋ถ€ํ„ฐ article(instance) ์ƒ์„ฑ!
article = Article()
article
>>> <Article: Article object (None)>

article.title = 'first' # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(title)์— ๊ฐ’์„ ํ• ๋‹น
article.content = 'django!' # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(content)์— ๊ฐ’์„ ํ• ๋‹น

# save ๋ฅผ ํ•˜๊ณ  ํ™•์ธํ•˜๋ฉด ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค
article.save()
article
>>> <Article: Article object (1)>
Article.objects.all()
>>> <QuerySet [Article: Article object (1)]>

# ์ธ์Šคํ„ด์Šค์ธ article์„ ํ™œ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•ด๋ณด์ž(์ €์žฅ๋œ๊ฑธ ํ™•์ธ)
article.title
>>> 'first'
article.content
>>> 'django!'
article.created_at
>>> datetime.datetime(2019, 8, 21, 2, 43, 56, 49345, tzinfo=<UTC>)
  • ๋‘๋ฒˆ์งธ ๋ฐฉ์‹
    • ํ•จ์ˆ˜์—์„œ keyword ์ธ์ž๋ฅผ ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹๊ณผ ๋™์ผ
article = Article(title='second', content='django!!')

# ์•„์ง ์ €์žฅ์ด ์•ˆ๋˜์–ด ์žˆ์Œ
article
>>> <Article: Article object (None)>

# save๋ฅผ ํ•ด์ฃผ๋ฉด ์ €์žฅ์ด ๋œ๋‹ค.
article.save()
article
>>> <Article: Article object (2)>
Article.objects.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>]>
  • ์„ธ๋ฒˆ์งธ ๋ฐฉ์‹

    • create() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ์…‹ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋กœ์ง์ด ํ•œ๋ฒˆ์˜ ์Šคํ…์œผ๋กœ ๊ฐ€๋Šฅ
Article.objects.create(title='third', content='django!')
>>> <Article: Article object (3)>

โ—พ Read

all()

์ฐธ๊ณ 

  • QuerySet return
  • ๋ฆฌ์ŠคํŠธ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘
Article.objects.all()
>>> <QuerySet [<Article: Article object (1)>, <Article: Article object (2)>, <Article: Article object (3)>, <Article: Article object (4)>]>

get()

์ฐธ๊ณ 

  • ๊ฐ์ฒด๊ฐ€ ์—†์œผ๋ฉด DoesNotExist ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๊ณ  ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ์— MultipleObjectReturned ์˜ค๋ฅ˜๋ฅผ ๋„์›€.
  • ์œ„์™€ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— unique ํ˜น์€ Not Null ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์„๋•Œ์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
article = Article.objects.get(pk=100)
>>> DoesNotExist: Article matching query does not exist.

Article.objects.get(content='django!')
>>> MultipleObjectsReturned: get() returned more than one Article -- it returned 2!
**filter()**

filter()

์ฐธ๊ณ 

  • ์ง€์ •๋œ ์กฐํšŒ ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ QuerySet์„ ๋ฐ˜ํ™˜
Article.objects.filter(content='django!')
>>> <QuerySet [<Article: first>, <Article: fourth>]>

Article.objects.filter(title='first')
>>> <QuerySet [<Article: first>]>

The pk lookup shortcut

์ฐธ๊ณ 

  • ์šฐ๋ฆฌ๊ฐ€ .get(id=1) ํ˜•ํƒœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ .get(pk=1) ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š”(DB์—๋Š” id๋กœ ํ•„๋“œ ์ด๋ฆ„์ด ์ง€์ • ๋จ์—๋„) .get(pk=1) ์ด.get(id__exact=1) ์™€ ๋™์ผํ•œ ์˜๋ฏธ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • pk๋Š” id__exact ์˜ shortcut ์ด๋‹ค.

Blog.objects.get(id__exact=14) # Explicit form
Blog.objects.get(id=14) # __exact is implied
Blog.objects.get(pk=14) # pk implies id__exact

โ—พ Update

  • article ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด ์ƒ์„ฑ
  • article.title = 'byebye' : article ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜์—ฌ ๊ธฐ์กด์˜ ๊ฐ’์„ byebye ๋กœ ๋ณ€๊ฒฝ
  • article.save() : article ์ธ์Šคํ„ด์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ save() ๋ฉ”์†Œ๋“œ ์‹คํ–‰
# UPDATE articles SET title='byebye' WHERE id=1;
article = Article.objects.get(pk=1)
article.title
>>> 'first'

# ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์ €์žฅ
article.title = 'byebye'
article.save()

# ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธ
article.title
>>> 'byebye'

โ—พ Delete

  • article ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ›„ .delete() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
article = Article.objects.get(pk=1)

# ์‚ญ์ œ
article.delete()
>>> (1, {'articles.Article': 1})

# ๋‹ค์‹œ 1๋ฒˆ ๊ธ€์„ ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋ฉด ์—†๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค.
Article.objects.get(pk=1)
>>> DoesNotExist: Article matching query does not exist.
profile
๊ธฐ๋กํ•˜๋Š” ์Šต๊ด€์€ ์‰ฝ๊ฒŒ ๋ฌด๋„ˆ์ง€์ง€ ์•Š์•„์š”.

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