484π
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.
49π
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.
- [Django]-Access web server on VirtualBox/Vagrant machine from host browser?
- [Django]-Django-Forms with json fields
- [Django]-How can I see the raw SQL queries Django is running?
16π
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.
- [Django]-UUID as default value in Django model
- [Django]-Django middleware difference between process_request and process_view
- [Django]-How can I access environment variables directly in a Django template?
15π
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;
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-Django β how to unit test a post request using request.FILES
- [Django]-How do you Serialize the User model in Django Rest Framework
5π
TextField can contain more than 255 characters, but CharField is used to store shorter length of strings.
When you want to store long text, use TextField, or when you want shorter strings then CharField is useful.
article_title = models.CharField(max_length=150)
article_body = models.TextField()
- [Django]-How to run celery as a daemon in production?
- [Django]-Django admin TabularInline β is there a good way of adding a custom html column?
- [Django]-Django models avoid duplicates