django -- verbose_name的对数据库层面的影响
一、没有verbose_name时model的定义:
from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=128) class Answer(models.Model): question = models.ForeignKey(Question) answer_text = models.CharField(max_length=128)
二、对应的SQL:
BEGIN; -- -- Create model Answer -- CREATE TABLE "polls_answer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "answer_text" varchar(128) NOT NULL); -- -- Create model Question -- CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(128) NOT NULL); -- -- Add field question to answer -- ALTER TABLE "polls_answer" RENAME TO "polls_answer__old"; CREATE TABLE "polls_answer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "answer_text" varchar(128) NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id")); INSERT INTO "polls_answer" ("id", "answer_text", "question_id") SELECT "id", "answer_text", NULL FROM "polls_answer__old"; DROP TABLE "polls_answer__old"; CREATE INDEX "polls_answer_question_id_d45b3e6d" ON "polls_answer" ("question_id"); COMMIT;
三、带有verbose_name的model定义:
from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=128) class Answer(models.Model): question = models.ForeignKey(Question,verbose_name="related question id") answer_text = models.CharField(max_length=128)
四、对应SQL层的代码:
BEGIN; -- -- Create model Answer -- CREATE TABLE "polls_answer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "answer_text" varchar(128) NOT NULL); -- -- Create model Question -- CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(128) NOT NULL); -- -- Add field question to answer -- ALTER TABLE "polls_answer" RENAME TO "polls_answer__old"; CREATE TABLE "polls_answer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "answer_text" varchar(128) NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id")); INSERT INTO "polls_answer" ("id", "answer_text", "question_id") SELECT "id", "answer_text", NULL FROM "polls_answer__old"; DROP TABLE "polls_answer__old"; CREATE INDEX "polls_answer_question_id_d45b3e6d" ON "polls_answer" ("question_id"); COMMIT;
五、总结:
由上面的两组代码可以看出verbose_name 在SQL层面没有具体的体现、也就是说加不加verbose_name
对数据库层面没影响。
----