장고/점프투장고

점프 투 장고. 재실습

나도초딩 2023. 11. 11.

python manage.py sqlmigrate b2b 0001

python manage.py makemigrations

migrate 하기 전에 쿼리문을 살펴볼 수 있다.

(venv38) {0:30}~/github/tws ➭ python manage.py sqlmigrate b2b 0001
BEGIN;
--
-- Create model Question
--
CREATE TABLE "b2b_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "subject" varchar(200) NOT NULL, "content" text NOT NULL, "create_date" datetime NOT NULL);
--
-- Create model Answer
--
CREATE TABLE "b2b_answer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "subject" varchar(200) NOT NULL, "content" text NOT NULL, "create_date" datetime NOT NULL, "question_id" bigint NOT NULL REFERENCES "b2b_question" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "b2b_answer_question_id_9d191ec8" ON "b2b_answer" ("question_id");
COMMIT;

 

 

shell 로 Question 객체(테이블)에  레코드 추가.

python.manage.py shell

 

from b2b.models import Question, Answer

from django.utils import timezone

 

q = Question(subject = '제목', content = '내용', create_date = timezone.now())

q.save()

 

Question 객체 조회 :: Question.objects.all()

def __str__(self):  을 추가하면, 클래스 조회시, 기본값인 id 대신, 제목과 필요한 값을 보여줄 수 있다.

댓글