SQL Django CharField 与 TextField
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7354588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django CharField vs TextField
提问by Jonathan Gleason
What is the difference between CharField()
and TextField()
in Django? The documentationsays that CharField()
should be used for smaller strings and TextField()
should be used for larger strings. Okay, but where is the line drawn between "small" and "large"? What's going on under the hood here that makes this the case?
DjangoCharField()
和TextField()
Django 中有什么区别?该文件说,CharField()
应该用于更小的字符串,TextField()
应使用较大的字符串。好的,但是“小”和“大”之间的界线在哪里?是什么让这一切成为现实?
回答by Cat Plus Plus
It's a difference between RDBMS's varchar
(or similar) — those are usually specified with a maximum length, and might be more efficient in terms of performance or storage — and text
(or similar) types — those are usually limited only by hardcoded implementation limits (not a DB schema).
这是 RDBMS varchar
(或类似的)之间的区别——那些通常用最大长度指定,并且在性能或存储方面可能更有效——和text
(或类似的)类型——它们通常只受硬编码实现限制(不是数据库架构)。
PostgreSQL 9, specifically, states that "There is no performance difference among these three types", but AFAIK there are some differences in e.g. MySQL, so this is something to keep in mind.
PostgreSQL 9 明确指出“这三种类型之间没有性能差异”,但 AFAIK 在例如 MySQL 中存在一些差异,因此需要记住这一点。
A good rule of thumb is that you use CharField
when you need to limit the maximum length, TextField
otherwise.
一个好的经验法则是CharField
在需要限制最大长度时使用,TextField
否则使用。
This is not really Django-specific, also.
这也不是真正特定于 Django 的。
回答by renderbox
In some cases it is tied to how the field is used. In some DB engines the field differences determine how (and if) you search for text in the field. CharFields are typically used for things that are searchable, like if you want to search for "one" in the string "one plus two". Since the strings are shorter they are less time consuming for the engine to search through. TextFields are typically not meant to be searched through (like maybe the body of a blog) but are meant to hold large chunks of text. Now most of this depends on the DB Engine and like in Postgres it does not matter.
在某些情况下,它与字段的使用方式有关。在某些数据库引擎中,字段差异决定了您如何(以及是否)在字段中搜索文本。CharFields 通常用于可搜索的内容,例如您想在字符串“一加二”中搜索“一”。由于字符串较短,因此引擎搜索所需的时间较少。TextFields 通常不是用来搜索的(比如博客的正文),而是用来保存大块文本。现在这大部分取决于数据库引擎,就像在 Postgres 中一样,这无关紧要。
Even if it does not matter, if you use ModelForms you get a different type of editing field in the form. The ModelForm will generate an HTML form the size of one line of text for a CharField and multiline for a TextField.
即使没有关系,如果您使用 ModelForms,您也会在表单中获得不同类型的编辑字段。ModelForm 将生成一个 HTML 表单,其大小为 CharField 的一行文本和 TextField 的多行文本。
回答by SuperNova
For eg.,. 2 fields are added in a model like below..
例如,。在如下模型中添加了 2 个字段。
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.
以下是应用迁移时执行的 mysql 查询。
for TextField
(description) the field is defined as a longtext
对于TextField
(描述)该字段被定义为longtext
ALTER TABLE `sometable_sometable` ADD COLUMN `description` longtext NULL;
The maximum length of TextField
of MySQL is 4GB according to string-type-overview.
TextField
根据string-type-overview,MySQL的最大长度为 4GB 。
for CharField
(title) the max_length(required) is defined as varchar(64)
对于CharField
(title) max_length(required) 定义为varchar(64)
ALTER TABLE `sometable_sometable` ADD COLUMN `title` varchar(64) NULL;
ALTER TABLE `sometable_sometable` ALTER COLUMN `title` DROP DEFAULT;
回答by Njeru Cyrus
CharField
has max_length of 255
characters while TextField
can hold more than 255
characters. Use TextField
when you have a large string as input. It is good to know that when the max_length
parameter is passed into a TextField
it passes the length validation to the TextArea
widget.
CharField
有 max_length 个255
字符,而TextField
可以容纳多个255
字符。使用TextField
时,你有一个大的字符串作为输入。很高兴知道,当max_length
参数传入 a 时,TextField
它会将长度验证传递给TextArea
小部件。
回答by amin_mirr
I had an strange problem and understood an unpleasant strange difference:
when I get an URL from user as an CharFieldand then and use it in html a tag by href, it addsthat url to my url and that's not what I want. But when I do it by Textfieldit passes justthe URL that user entered.
look at these:
my website address: http://myweb.com
我遇到了一个奇怪的问题,并且理解了一个令人不快的奇怪差异:当我从用户那里获取一个 URL 作为CharField然后并在 html 中使用它作为标签的 href 时,它会将该 url添加到我的 url 中,这不是我想要的。但是,当我通过做文本字段它通过只是用户输入的网址。看看这些: 我的网址:http://myweb.com
CharField entery: http://some-address.com
CharField 输入: http://some-address.com
when clicking on it: http://myweb.comhttp://some-address.com
点击它时: http://myweb.comhttp://some-address.com
TextField entery: http://some-address.com
文本字段输入: http://some-address.com
when clicking on it: http://some-address.com
点击它时: http://some-address.com
I must mention that the URL is saved exactly the samein DB by two ways but I don't know why result is different when clicking on them
不得不提的是,URL保存完全一样通过两种方式在DB,但我不知道为什么他们点击时的结果是不同的