In Django, the TextField
is a field type used to store longer text strings, such as paragraphs, articles, or any sizable text content. It is designed for situations where you need to store large amounts of text that may exceed the maximum length supported by a CharField
.
Lets create a model to show the use of TextField
.
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
You need to run the python manage.py makemigrations
and python manage.py migrate
commands to create a model.
python manage.py makemigrations
python manage.py migrate
Parameters and Attributes:
-
blank: A boolean parameter that, when set to
True
, allows the field to be left blank (i.e., it's not required). The default isFalse
. -
null: A boolean parameter that, when set to
True
, allows the field to have aNULL
database value. The default isFalse
. - default: This parameter allows you to set a default value for the field. The default value will be used when creating a new instance of the model if the field is not explicitly set. It is the same as you saw in the CharField.
from datatypedemo.models import Article
a=Article.objects.create(title='Test', content='Edkool\'s Article')
vars(a)