Django CharField vs TextField

后端 未结 5 1095
耶瑟儿~
耶瑟儿~ 2020-12-04 06:13

What is the difference between CharField() and TextField() in Django? The documentation says that CharField() should be used for smal

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 06:43

    For eg.,. 2 fields are added in a model like below..

    description = models.TextField(blank=True, null=True)
    title = models.CharField(max_length=64, blank=True, null=True)
    

    Below are the mysql queries executed when migrations are applied.


    for TextField(description) the field is defined as a longtext

    ALTER TABLE `sometable_sometable` ADD COLUMN `description` longtext NULL;
    

    The maximum length of TextField of MySQL is 4GB according to string-type-overview.


    for CharField(title) the max_length(required) is defined as varchar(64)

    ALTER TABLE `sometable_sometable` ADD COLUMN `title` varchar(64) NULL;
    ALTER TABLE `sometable_sometable` ALTER COLUMN `title` DROP DEFAULT;
    

提交回复
热议问题