Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's 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?

like image 491
Jonathan Gleason Avatar asked Sep 08 '11 21:09

Jonathan Gleason


People also ask

What does CharField mean in django?

CharField is a commonly-defined field used as an attribute to reference a text-based database column when defining Model classes with the Django ORM. The Django project has wonderful documentation for CharField and all of the other column fields.

What is a TextField django?

TextField is a field used to create an arbitrary amount of text characters in a database column defined by the Django ORM. The Django project has wonderful documentation for TextField and all of the other column fields. Note that TextField is defined within the django.

What is the max length of CharField django?

CharField since CharField has a max length of 255 characters while TextField can hold more than 255 characters.


1 Answers

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.

like image 200
Cat Plus Plus Avatar answered Oct 19 '22 08:10

Cat Plus Plus