zoukankan      html  css  js  c++  java
  • 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

      对数据库层面没影响。

    ----

  • 相关阅读:
    【转载】[SMS]SMS内容的7bit和UCS2编码方式简介
    【转载】两篇关于字符编码的博文
    【IRA/GSM/UCS2】the difference of IRA/GSM/UCS2 character set
    【LTE】LTE中SINR的理解
    【LTE】为什么使用SNR来表征信道质量,而并不用RSRQ?这两者的区别是什么?
    【C++】C++为什么要引入引用这个复合类型?
    【HTML55】HTML5与CSS3基础教程
    python 三种单例模式
    python3.10 新增的 match case 语句
    Python pyqt5简单的图片过滤工具
  • 原文地址:https://www.cnblogs.com/JiangLe/p/7920214.html
Copyright © 2011-2022 走看看