반응형
![](https://blog.kakaocdn.net/dn/mu4c0/btrsyFhYbTa/f2ZeXrVBeTcGigQ5fVidbk/img.png)
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
반응형
'Django' 카테고리의 다른 글
django db admin 계정 사용하기. create user (0) | 2022.02.08 |
---|---|
Python Shell 이용하여 명령어 실행시키기 (0) | 2022.02.08 |
Django sqlite3 기초 (0) | 2022.02.08 |
Django에서 Static File 읽어오기 (0) | 2022.02.08 |
Shell을 이용하여 가상환경 바로 들어가기 (0) | 2022.02.08 |