new_project
이름의 Django 프로젝트를 하나 생성합니다.
$ django-admin startproject new_project .
test_app
이름의 앱을 생성합니다.
$ python manage.py startapp test_app
test_app
폴더 안에 models.py
파일을 열어서 모델을 생성합니다.
📁 test_app/models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=50)
author = models.CharField(max_length=20)
content = models.TextField()
created = models.DataTimeField(auto_now_add=True)
updated = models.DataTimeField(auto_now=True)
auto_now_add
: 처음 레코드 생성 시점auto_now
: 마지막으로 저장된 시점
📁new_project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app',
]
하단에 test_app
앱을 추가합니다.
settings.py에 앱을 등록하지 않으면 models.py 수정을 감지하지 못합니다.
$ python manage.py makemigrations
migrations 폴더에 새로운 파일이 생성됩니다.
실제 데이터베이스에 모델을 적용하려면 하단 명령어를 실행합니다.
$ python manage.py migrate