Django ๊ธฐ์ดˆ - Django ORM(QuerySet)

ID์งฑ์žฌยท2021๋…„ 7์›” 22์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
8/43
post-thumbnail

๐ŸŒˆ Django ORM(QuerySet)

๐Ÿ”ฅ ์กฐํšŒ(READ)

๐Ÿ”ฅ ์ƒ์„ฑ(CREATE)

๐Ÿ”ฅ ์ˆ˜์ •(Update)

๐Ÿ”ฅ ์‚ญ์ œ(Delete)


ORM์ด๋ž€ ๋ชจ๋ธ ํด๋ž˜์Šค(models.py)๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ๊ธฐ์ˆ ๋กœ ์ง์ ‘ spl๋ฌธ์„ ํ†ตํ•ด ์กฐ์ž‘ํ•  ํ•„์š”์—†์ด python๋ฌธ๋ฒ•์„ ํ†ตํ•ด DB๋ฅผ ์ œ์–ด(Create, Read, Update, Delete)ํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ์ˆ ์ด์—์š”!
Django Shell์—์„œ ์ด๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ• ๊ป˜์š”:)

  • Django Shell ์ง„์ž… ๋ช…๋ น : ๐Ÿ” python manage.py shell
  • Django shell ํƒˆ์ถœ ๋ช…๋ น : ๐Ÿ” exit()
  • Model import ๋ฐฉใ…“ใ…‚
    • 1) ๐Ÿ” from [App์ด๋ฆ„].models import [Model Class]
    • 2) ๐Ÿ” from [App์ด๋ฆ„] import models
    • 3) ๐Ÿ” from [App์ด๋ฆ„] import models as [๋‹ค๋ฅธ์ด๋ฆ„]

1. ์กฐํšŒ(READ)

  • ๋ชจ๋“  ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.all()
  • ๋ชจ๋“  ๊ฐ์ฒด ์—ญ์ˆœ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.all().order_by('-pk')
  • ๋ชจ๋“  ๊ฐ์ฒด ๊ฐฏ์ˆ˜ ํ™•์ธ : ๐Ÿ” Notice.objects.count()
  • ํŠน์ • ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ• : ๐Ÿ” Notice.objects.get(id=1) ๋˜๋Š” ๐Ÿ” Notice.objects.get(pk=1)
  • ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter()
    • viewCount๊ฐ€ 20์ธ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter(viewCount=20)
    • likeCount๊ฐ€ 5๋ณด๋‹ค ์ž‘์€ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter(likeCount__lt=5)
    • likeCount๊ฐ€ 10 ์ด์ƒ์ธ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter(likeCount__gte=10)
    • title์ด 'test5'์ธ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter(title='test5')
    • title์— 'test'๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.filter(title__contains='test')
  • ์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.exclude()
    • title์ด 'test3'์ธ ๊ฐ์ฒด๋ฅผ ์ œ์™ธํ•˜๊ณ  ๊ฐ€์ ธ์˜ค๊ธฐ : ๐Ÿ” Notice.objects.exclude(title='test3')

2. ์ƒ์„ฑ(CREATE)

  • python shell์„ ํ†ตํ•ด์„œ๋„ admin site์™€ ๊ฐ™์ด DB์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์–ด์š”.
  • ๊ฐ์ฒด์ด๋ฆ„.objects.create(ํ•„๋“œ1='๊ฐ’', ํ•„๋“œ2='๊ฐ’', ํ•„๋“œ3='๊ฐ’', ํ•„๋“œ4='๊ฐ’')
    • ๐Ÿ” q= Notice.objects.create(title='test7', likeCount=100, viewCount=100, contents='test7 contents.....')
  • shell ์ข…๋ฃŒ ์ „, save() ๋งค์„œ๋“œ๋กœ ์ €์žฅํ•˜์ง€ ์•Š์œผ๋ฉด ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์–ด์š”.
    • q.save()

3. ์ˆ˜์ •(Update)

  • ์ˆ˜์ •์€ ์ˆ˜์ •ํ•  row๋ฅผ ์ง€์ •ํ•œ ๋’ค, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•˜๋“ฏ์ด ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • test7์˜ contents ๊ฐ’์„ 'Hello Django'๋กœ ์ˆ˜์ •ํ•ด๋ณผ๊ป˜์š”:)
    • q = Notice.objects.get(title='test7')
    • q.contents = 'Hello Django'
  • ์ˆ˜์ •ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ save() ๋งค์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • q.save() ๐Ÿ‘ˆ save()์•ˆํ•˜๋ฉด ์ˆ˜์ • ์•ˆ๋จ

4. ์‚ญ์ œ(Delete)

  • ๋ฐ์ดํ„ฐ ์‚ญ์ œ ๋ฐฉ๋ฒ• : ์‚ญ์ œํ•  ๊ฐ์ฒด ์ง€์ • ํ›„ ๊ฐ์ฒด.delete()
  • ์ œ๋ชฉ์ด 'title7'์ธ ๊ฐ์ฒด ์‚ญ์ œ
  • ๐Ÿ” q = Notice.objects.get(title='test7') ๐Ÿ‘ˆ ์‚ญ์ œ ๋Œ€์ƒ ์ง€์ •
  • ๐Ÿ” q.delete() ๐Ÿ‘ˆ ์‚ญ์ œ
profile
Keep Going, Keep Coding!

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