Notice
Recent Posts
Recent Comments
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฒ์ž๋ผ๋‚˜๋Š”์ฒญ๋…„

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ ํŠœํ† ๋ฆฌ์–ผ 2-1 ๋ณธ๋ฌธ

django

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ ํŠœํ† ๋ฆฌ์–ผ 2-1

JihyunLee 2019. 9. 17. 15:38
๋ฐ˜์‘ํ˜•

https://docs.djangoproject.com/ko/2.2/intro/tutorial02/

 

์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ, part 2 | Django ๋ฌธ์„œ | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

 

1. DB ์—ฐ๊ฒฐํ•˜๊ธฐ

์žฅ๊ณ ๋Š” sqlite๋ผ๋Š” ์ž‘์€ ๋””๋น„ ์ €์žฅ์†Œ๊ฐ€ ์ด๋ฏธ ์„ค์น˜๋˜์–ด ์žˆ๋‹ค. ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผํ•˜๋Š” ๊ฒƒ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, ๋‚˜์ค‘์— ์•ฑ์ด ์ปค์งˆ๊ฒƒ์„ ์ƒ๊ฐํ•˜์—ฌ ๋‹ค๋ฅธ db๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋„ ๊ทธ๋ ‡๊ฒŒ ๋งํ–ˆ๋‹ค.

 

์—ฌํŠผ ์žฅ๊ณ ๋Š” ์ด๋ฏธ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์™€ ์—ฐ๋™๋˜์–ด ์žˆ๋‹ค.

 

์žฅ๊ณ ๋ฅผ ๋‹ค๋ฃจ๋ฉฐ ์ข…์ข… ๋“ฃ๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์„ ์ ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ์“ธ์ธ๋‹ค(์•„๋งˆ๋„..?)

 

๋˜ํ•œ models.py๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ–ˆ๋‹ค.

 

2.  models.py ๋งŒ๋“ค๊ธฐ

๊ธฐ๋ณธ SQLite๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด mysite/settings.py๋ฅผ ๋”ฐ๋กœ ์ˆ˜์ • ํ•  ํ•„์š” ์—†์ด polls/models.py ๋ถ€ํ„ฐ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
from django.db import models
 
 
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
 
 
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
cs

์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, MYSQL์—์„œ์ฒ˜๋Ÿผ, Question, Choice ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  Question์•ˆ์—๋Š” question_text์™€ pub_date column์„ ๋‘” ๊ฒƒ์œผ๋กœ ํ•ด์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Choice ํ…Œ์ด๋ธ”๋„ question์€ Questionํ…Œ์ด๋ธ”์„ ForeignKey๋กœ ๋‘๊ณ  (์•„๋ฌด๋ž˜๋„index๊ฒ ์ง€?) ๋‹ค๋ฅธ column๋“ค์„ ๋งŒ๋“  ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

polls/models.py๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ ํ•œ ๋‹ค์Œ์— poll์ด๋ผ๋Š” ์•ฑ์ด ์ƒˆ๋กœ ์ƒ๊ฒผ๋‹ค๋Š” ๊ฒƒ์„ ํ”„๋กœ์ ํŠธ์— ํฌํ•จ์‹œ์ผœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

mysite/settings.py์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

3. ๋ณ€๊ฒฝ์‚ฌํ•ญ mygrate

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
cs

๊ทธ ๋’ค์— ๋‹ค์Œ migrate์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ €์žฅ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋Š” ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ ์‹œํ‚ค๊ณ 

1
$ python manage.py makemigrations polls
cs
migrate ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
1
$ python manage.py migrate
cs

 

 

๋ฐ˜์‘ํ˜•