본문 바로가기

Django

Django Model 생성하기

반응형

 

이미지 썸네일 삭제
Writing your first Django app, part 2 | Django documentation | Django

Writing your first Django app, part 2 ¶ This tutorial begins where Tutorial 1 left off. We’ll set up the database, create your first model, and get a quick introduction to Django’s automatically-generated admin site. Where to get help: If you’re having trouble going through this tutorial, please hea...

docs.djangoproject.com

1. app에 이미 생성된 models.py에 아래와 같이 코딩한다.

from django.db import models

# Create your models here.
class Post(models.Model):
    post_title = models.CharField(max_length=200)
    post_subtitle = models.CharField(max_length=200)
    post_contents = models.CharField(max_length=3000)
 

2. Django에게 model이 변경되었음을 알려주기 위한 명령어를 수행한다.

$ python manage.py makemigrations
Migrations for 'HelloWorld':
  HelloWorld/migrations/0001_initial.py
    - Create model Post
 

3. 사용 될 Query 확인하기

$ python manage.py sqlmigrate HelloWorld 0001
BEGIN;
--
-- Create model Post
--
CREATE TABLE "HelloWorld_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "post_title" varchar(200) NOT NULL, "post_subtitle" varchar(200) NOT NULL, "post_contents" varchar(3000) NOT NULL);
COMMIT;
 

4. 생성하기

$ python manage.py migrate
Operations to perform:
  Apply all migrations: HelloWorld, admin, auth, contenttypes, sessions
Running migrations:
  Applying HelloWorld.0001_initial... OK
 

 

 

반응형