backend/django

django tutorial: 데이터베이스 셋업, 모델 만들어서 migrate 해보기 (part2)

seul chan 2017. 3. 1. 01:26

# https://docs.djangoproject.com/en/1.10/intro/tutorial02/


# setup database

# create first model

# Django의 자동 admin 사이트


*Databasse setup

# 기본적인 설정으로 SQLite가 제공 => 가장 쉬운 방법, 다른 설정 할 필요 없음

                             # => 실제 프로젝트엔 부적합 하다고 한다.

# 나는 postgresql 로 세팅하여 진행하였다. 세팅 방법은 (블로그에 추후 올리겠음: http://seulcode.tistory.com/111) 참고하면 된다


*INSTALLED_APPS => 현재 activate 된 장고 애플리케이션의 목록

# 앱들은 여러개의 프로젝트에서 사용 가능 => 묶고 분배할 수 있다.


*기본 INSTALLED_APPS

- django.contrib.admin: admin site

- django.contrib.auth: authentication 시스템

- django.contrib.contenttypes: 컨텐츠 타입의 프레임워크

- django.contrib.sessions: 세션 프레임워크

- django.contrib.message: 메시징 프레임워크

- django.contrib.staticfiles: 통계 파일을 다루는 프레임워크

# 내가 설명을 대충한 게 아니라 이렇게 설명이 되어있다... 자세한 내용은 장고 사이트에서 더 나와있다. (https://docs.djangoproject.com/en/1.10/intro/tutorial02/)


*table 만들기 => 몇몇 app들은 최소한 한개의 database table이 필요하다

# 이를 위해서 다음 코드 실행:

$python manage.py migrate

# mirgrate => INSTALLED_APPS들을 보고 이에 필요한 database table들을 생성

------------------

Applying admin.0002_logentry_remove_auto_add... OK

Applying contenttypes.0002_remove_content_type_name... OK

Applying auth.0002_alter_permission_name_max_length... OK

Applying auth.0003_alter_user_email_max_length... OK

Applying auth.0004_alter_user_username_opts... OK

Applying auth.0005_alter_user_last_login_null... OK

Applying auth.0006_require_contenttypes_0002... OK

Applying auth.0007_alter_validators_add_error_messages... OK

Applying auth.0008_alter_user_username_max_length... OK

Applying sessions.0001_initial... OK

------------------

# 어찌고저찌고... OK 라고 쭉 뜨면 성공한 것이다.



*models 만들기

*철학

# A model is the single, definitive source of truth about your data

# 저장하는 데이터의 필수적인 필드와 behaviors(?)를 저장

# DRY principle (Don't repeat yourself)

Every distinct concept and/or piece of data should live in one, and only one, place. Redundancy is bad. Normalization is good.

The framework, within reason, should deduce as much as possible from as little as possible.

(좋은 말인것같다)

# data model을 한 장소에 정의 => 이를 자동으로 derive


*poll app 만들기

예제에서는 => Question & Choice 두개의 심플한 모델을 만들것이다.

# Question은 데이터 질문

# Choice는 두 필드 중 선택할 수 있는 기능을 갖는다.


*polls/models.py를 수정

-----------------------------

from django.db import models


class Question(models.Model):

    question_text = models.CharField(max_length=200)

    pub_date = models.DateTimeField('date published')



class Choice(models.Model):

    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    choice_text = models.CharField(max_length=200)

    votes = models.IntegerField(default=0)

------------------------------

# straightforward한 코드랜다.

# 각각의 model =>  django.db.models.Model의 subclass들

# 각각의 field => field 클래스의 인스턴스 => 장고에게 어떤 타입의 데이터가 오는지 알려줌

# field name (eg: question_text) => 필드 이름, 파이썬 코드에서 활용하고 database에서 column name으로 사용

# 필드의 첫번째 argument => human-readable name, 지정하지 않으면 장고가 자동으로 naming

# ex) put_date의 'data published', 나머지는 machine-readable name이 대체한다.


# 몇몇의 field=> 필수적인 arguments를 갖는다.

# ex) CharField: max_length


# Optional arguments도 있다

# ex) dafault=0


# ForeignKey => 관계를 명시해줌 (어떤 방식인지?)

# 장고에게 각각의 Choice가 하나의 Question과 연관되었다고 알려줌

# => 장고는 모든 데이터베이스 관계를 지원: many-to-many, many-to-one, one-to-one


*Activation models

코드가 장고에게 전달하는것 

=> 데이터베이스 스케마 생성 (CREATE TABLE statements)

=> Python database-access API 생성


*앱 철학

장고의 앱은 'Pluggable'하다 => 여러개의 프로젝트에 사용 가능하고, 앱들을 분배할 수 있다. 


*앱을 프로젝트에 설치하기

-INSTALLED_APPS 세팅 

=> PollsConfig 클래스는 polls/apps.py에 있으므로

=> 'polls.apps.PollsConfig'를 추가

=> 이제 mysite 프로젝트에서 polls 앱이 추가됨

$python manage.py makemigrations polls

# 아래 내용이 나오면 성공 한 것이다.

----------------------------------

Migrations for 'polls':

  polls/migrations/0001_initial.py:

    - Create model Choice

    - Create model Question

    - Add field question to choice

---------------------------------


*makemigrations 명령어 

=> 모델들에 변화가 생긴 것을 장고에게 알려줌

=> 이 변화를 저장하라는 명령

=> how Django changes to your models

=> polls/migrations/0001_initial.py 에서 migrations 된 것을 확인 가능하다


*sqlmigrate 명령어 (실제 데이터베이스에 적용하는 것은 아님)

$python manage.py sqlmigrate polls 0001

=> migration name를 받아서 SQL로 return해줌

---------------------------

BEGIN;

--

-- Create model Choice

--

CREATE TABLE "polls_choice" (

    "id" serial NOT NULL PRIMARY KEY,

    "choice_text" varchar(200) NOT NULL,

    "votes" integer NOT NULL

);

--

-- Create model Question

--

CREATE TABLE "polls_question" (

    "id" serial NOT NULL PRIMARY KEY,

    "question_text" varchar(200) NOT NULL,

    "pub_date" timestamp with time zone NOT NULL

);

--

-- Add field question to choice

--

ALTER TABLE "polls_choice" ADD COLUMN "question_id" integer NOT NULL;

ALTER TABLE "polls_choice" ALTER COLUMN "question_id" DROP DEFAULT;

CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");

ALTER TABLE "polls_choice"

  ADD CONSTRAINT "polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id"

    FOREIGN KEY ("question_id")

    REFERENCES "polls_question" ("id")

    DEFERRABLE INITIALLY DEFERRED;


COMMIT;

------------------------------------------------------------

-위 결과는 장고가 만들어낸 SQL 문이다

-이는 databases의 종류에 따라 다르다. (postgreSQL 사용)

-테이블명은 앱 이름과 연계하여 자동으로 생성 => polls

-lowercase는 question, choice (override 가능)

-Primary keys(IDs)는 자동으로 더해짐

-편의를 위해서 "_id"를 foreign key 필드 이름에 더함

-The foreign key relationship is made explicit by a FOREIGN KEY constraint. Don’t worry about the DEFERRABLE parts; that’s just telling PostgreSQL to not enforce the foreign key until the end of the transaction. (무슨말이지)

-sqlmigrate 코멘드는 실제로 데이터베이스에 migration 하지 않는다. 

=> 단지 어떤 SQL이 장고가 필요하다고 생각하는지 스크린에 보여줄 뿐! (엄청나다 장고..)


*migrate 

=> 실제 데이터베이스에 model tables을 생성!

$python manage.py migrate

--------------------

Operations to perform:

  Apply all migrations: admin, auth, contenttypes, polls, sessions

Running migrations:

  Rendering model states... DONE

  Applying polls.0001_initial... OK

-------------------

라는 말이 나오면 성공


migration은 데이터베이스에 적용되지 않은 것들을 싱크로나이징 시켜줌

3 step 기억하기

- changing your models (in models.py)

- $python manage.py makemigrations => 변경할 migrations 생성

- $python manage.py migrate => 변경 사항을 database에 적용



Playing with the API (부터는 내일부터 다시...) ~2/28