๐Ÿš€ Languege/Django-Python

[Django #4] Database ๊ตฌ์„ฑํ•˜๊ธฐ

mini_world 2021. 1. 19. 11:02
๋ชฉ์ฐจ ์ ‘๊ธฐ

 

์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ค์น˜ํ•˜๊ณ  ๋ชจ๋ธ์„ ์ •์˜/์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • ํŒŒ์ผ์ธ mysite/settings.py๋ฅผ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค :)

Django์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” SQLite๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์ง€๋งŒ, ์‹ค ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” postgresql๊ณผ ๊ฐ™์€ ์•ˆ์ •์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ์ง„ํ–‰ํ•ด๋ด…์‹œ๋‹ค ๊ณ ๊ณ !!๐Ÿง‘‍๐Ÿ’ป๐Ÿ‘ฉ‍๐Ÿ’ป

 


1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งŒ๋“ค๊ธฐ

๋จผ์ €, mysite/settings.py ํŒŒ์ผ์„ ์‚ดํŽด๋ด…์‹œ๋‹ค!

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.
์ง€๊ธˆ์€ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘ ๋ฉ”์ปค๋‹ˆ์ฆ˜๋งŒ ์‚ดํŽด๋ณผ ์˜ˆ์ •์ด๋ฏ€๋กœ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” SQLite๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค :)

Language_code์™€ Time_zone์„ ์•Œ๋งž๊ฒŒ ๋ฐ”๊ฟ”์ค์‹œ๋‹ค.  ;)

LANGUAGE_CODE = 'ko-KR'

TIME_ZONE = 'Asia/Seoul'

์ด์ œ, ๋จผ์ € ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค

๋จผ์ €, ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (pycharm ์™ผ์ชฝ ์•„๋ž˜ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค)

python manage.py migrate

manage.py ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ด€๋ฆฌ ๋ช…๋ น์–ด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ช…๋ น์–ด ์œ ํ‹ธ๋ฆฌํ‹ฐ์ž…๋‹ˆ๋‹ค.
python manage.py migrate ๋ช…๋ น์–ด๋Š” mysite/settings.py ํŒŒ์ผ์˜ INSTALLED_APPS ์™€ DATABASES ์„ค์ •์„ ํ™•์ธํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ์ฒ˜๋Ÿผ ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต์ž…๋‹ˆ๋‹ค :)

์ด์ œ Django ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ์ƒ์„ฑ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ€์„œ APP์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 


2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ(์Šคํ‚ค๋งˆ) ์ •์˜/์ƒ์„ฑ

์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” APP์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ตฌ์กฐ(์Šคํ‚ค๋งˆ)๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.

๋จผ์ €, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— 2๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค ์˜ˆ์ •์ž…๋‹ˆ๋‹ค :)

first_app/models.py ํŒŒ์ผ์— ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
models.py ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊ฒ ๋‹ค๋ผ๋Š” ์ •์˜๋ฅผ ํ•ด์ฃผ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์„ธ์š”!

import datetime
from django.db import models


# Create your models here.


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text
    datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

์ด์ œ first_app/models.py์— ๋ฐ์ดํ„ฐ์˜ ์Šคํ‚ค๋งˆ๊ฐ€ ์ •์˜ ๋˜์—ˆ๋‹ค๋Š”๊ฒƒ์„ ๋ฉ”์ธ ํ”„๋กœ์ ํŠธ์— ์•Œ๋ ค์ค์‹œ๋‹ค.

mysite/settings.pyํŒŒ์ผ์„ ๋‹ค์‹œ ์—ด์–ด์ค€๋‹ค์Œ, INSTALLED_APPS ์— first_app์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

INSTALLED_APPS = [                 
    'django.contrib.admin',       
    'django.contrib.auth',       
    'django.contrib.contenttypes', 
    'django.contrib.sessions',   
    'django.contrib.messages',     
    'django.contrib.staticfiles', 
    'first_app.apps.FirstAppConfig',
]

์ด์ œ ๋‘๋ฒˆ์งธ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ์‹œ๋‹ค!

python manage.py makemigrations first_app

first_app ๋ถ€๋ถ„๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰ ํ–ˆ์„ ์‹œ first_app/migrations ํด๋”์— 0001_initial.py๊ฐ€ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ์— ํ•„์š”ํ•œ ๋ถ€๋ถ„์ด ์ •์˜ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

์ด ํŒŒ์ผ๋“ค์ด ๋ฐ˜์˜ ๋˜๋„๋ก ๋งˆ์ง€๋ง‰ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ค๋‹ˆ๋‹ค.

python manage.py migrate

๋ช…๋ น์–ด ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์ ์šฉ๋˜์—ˆ๋‹ค๋Š” ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค :)

 ์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰ํ•˜์…จ๋‹ค๋ฉด ์™„๋ฃŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •๊นŒ์ง€ ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)


 

๋‘ ๋‹จ๊ณ„๋ฅผ ๋ชจ๋‘ ๋งˆ์น˜์…จ๋‹ค๋ฉด, ์ด๊ฒŒ ์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๊ฑด์ง€ ๊ถ๊ธˆํ•˜์‹ค ๊ฒ๋‹ˆ๋‹ค.
๋ˆˆ์œผ๋กœ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค. 

์•„๋ž˜ ์บก์ณ ์‚ฌ์ง„์€ SQLite Client๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ™•์ธํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. (SQLiteClient์„ค์น˜์•ˆ๋‚ด)

1๋‹จ๊ณ„์—์„œ ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ” ๊ทธ๋ฆฌ๊ณ  2๋‹จ๊ณ„์—์„œ ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ” (first_app_choice/ first_app_question) ์Šคํ‚ค๋งˆ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

 

๋ชจ๋“  ์‹ค์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)

 

๋ชจ๋“  ์‹ค์Šต์€ ๊ณต์‹ํ™ˆํŽ˜์ด์ง€์™€ ๋น„์Šทํ•˜๊ฒŒ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.!! 

728x90