Django CharField vs TextField [英] Django CharField vs TextField

查看:1767
本文介绍了Django CharField vs TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django中的 CharField() TextField()有什么区别? 文档表示 CharField()应用于较小的字符串,而 TextField()应用于较大的字符串。好的,但是在小和大之间画线呢?

解决方案

这是RDBMS的 varchar之间的区别(或类似) - 通常以最大长度指定,并且在性能或存储方面可能更有效 - 而 text (或类似的)类型 - 通常仅受硬编码实现限制(而不是DB模式)的限制。



PostgreSQL 9具体说,这三种类型之间没有性能差异,但是AFAIK在例如MySQL,所以这是要记住的一点。



一个很好的经验法则是你使用 CharField 您需要限制最大长度 TextField 否则。



这不是Django特有的,也是。 / p>

What is the difference between CharField() and TextField() in Django? The documentation says 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?

解决方案

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).

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.

A good rule of thumb is that you use CharField when you need to limit the maximum length, TextField otherwise.

This is not really Django-specific, also.

这篇关于Django CharField vs TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆