Django CharField vs TextField

后端 未结 5 1084
耶瑟儿~
耶瑟儿~ 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条回答
  •  失恋的感觉
    2020-12-04 06:16

    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.

    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.

提交回复
热议问题