>notepad models.py // ν
μ΄λΈ μ μ
>notepad admins.py // μ μλ ν
μ΄λΈμ΄ Admin νλ©΄μ 보μ΄κ² ν¨
>python manage.py makemigrations // DBμ λ³κ²½μ΄ νμν μ¬νμ μΆμΆ
>python manage.py migrate // DB λ³κ²½μ¬ν λ°μ
>python manage.py runserver // νμ¬κΉμ§ μμ
μ κ°λ°μ© μΉ μλ²λ‘ νμΈ
polls μ ν리μΌμ΄μ μ Questionκ³Ό Choice λ κ°μ ν μ΄λΈμ΄ νμνλ€. ν μ΄λΈμ models.py νμΌμ μ μνλ€.
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
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
models.pyμμ μ μν ν μ΄λΈλ Admin μ¬μ΄νΈμ 보μ΄λλ‘ admin.py νμΌμ λ±λ‘ν΄μ€λ€.
from django.contrib import admin
from ch3.polls.models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
models.py λͺ¨λμμ μ μν Question, Choice ν΄λμ€λ₯Ό importνκ³ admin.site.register() ν¨μλ₯Ό μ΄μ©νμ¬ μν¬νΈν ν΄λμ€λ₯Ό Admin μ¬μ΄νΈμ λ±λ‘ν΄μ€λ€.
μ΄μκ°μ΄ ν μ΄λΈμ μλ‘ λ§λ€ λλ models.pyμ admin.py λ κ°μ νμΌμ ν¨κΌ μμ ν΄μΌ νλ€.
ν μ΄λΈμ μ κ· μμ±, ν μ΄λΈμ μ μ λ³κ²½ λ± DBμ λ³κ²½μ΄ νμν μ¬νμ΄ μκΈ°λ©΄ λ°μν΄μ£Όμ΄μΌ νλ€.
>python manage.py makemigrations // DBμ λ³κ²½μ΄ νμν μ¬νμ μΆμΆ
>python manage.py migrate // DB λ³κ²½μ¬ν λ°μ
>python manage.py runserver
μΆμ²: Djangoλ‘ λ°°μ°λ νμ΄μ¬ μΉ νλ‘κ·Έλλ°(κΈ°μ΄) - κΉμνλ