django.db.models TextField Example Code

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.db.models.fields module but is typically referenced from django.db.models rather than including the fields module reference.

Example 1 from django-smithy

django-smithy is a code library for Django that makes it easy for users to send HTTP requests from the Django admin user interface. The code for the project is open source under the MIT license.

django-smithy / smithy /

# -*- coding: utf-8 -*-

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from requests import Request as HTTPRequest, Session
from requests.cookies import create_cookie, RequestsCookieJar
from urllib.parse import parse_qs, urlparse, urlencode
from requests_toolbelt.utils import dump

from model_utils.models import TimeStampedModel

from smithy.helpers import render_with_context, parse_dump_result

class NameValueModel(TimeStampedModel):
    name = models.CharField(max_length = 200)
    value = models.TextField(blank = True)

    def __str__(self):

    class Meta:
        abstract = True

class Request(TimeStampedModel):
    A base model shared by RequestBlueprint and
    RequestRecord. Used solely to reduce
    METHODS = (
        ('GET', 'GET'),
        ('POST', 'POST'),
        ('PUT', 'PUT'),
        ('DELETE', 'DELETE'),
        ('OPTIONS', 'OPTIONS'),
        ('HEAD', 'HEAD'),
    BODY_TYPES = (
        ('', 'Other'),
        ('application/x-www-form-urlencoded', 'x-www-form-urlencoded'),
        ('application/json', 'JSON'),
        ('text/plain', 'Text'),
        ('application/javascript', 'JavaScript'),
        ('application/xml', 'XML (application/xml)'),
        ('text/xml', 'XML (text/xml)'),
        ('text/html', 'HTML'),
    method = models.CharField(
        max_length = 7, choices = METHODS,
        blank = False, null = False)
    name = models.CharField(max_length = 500, blank = False)
    url = models.CharField(max_length = 2083)
    body = models.TextField(blank = True)
    content_type = models.CharField(
        default = BODY_TYPES[0][0],
        blank = True, null = True,
        max_length = 100, choices = BODY_TYPES)

    def __str__(self):
        return "{} {}".format(

class RequestBlueprint(Request):
    A blueprint for HTTP requests. This model will be
    used to generate and send HTTP requests. Once sent,
    a RequestRecord will be created for that request.
    follow_redirects = models.BooleanField(
        default = False, blank = False, null = False)

    def name_value_related(self):
        return [

    def send(self, context = None):

        context = context or {}
        for variable in self.variables.all():
            context[] = variable.value

        request = HTTPRequest(
            url = render_with_context(self.url, context),
            method = self.method)

        session = Session()
        record = RequestRecord.objects.create(blueprint = self)

        # Copy RequestBlueprint values to RequestRecord
        for qs in self.name_value_related:
            for obj in qs.all():
       = 0
       = render_with_context(, context)
                obj.value = render_with_context(obj.value, context)
                obj.request = record

        if self.content_type == self.BODY_TYPES[0][0]:
            data = render_with_context(self.body, context)
            data = {}
            for param in self.body_parameters.all():
                param.add_to(data, context) = data
        prepared_request = request.prepare()

        response = session.send(prepared_request, stream = True)
        # TODO: follow redirects

        RequestRecord.objects.filter(pk =
            raw_request = parse_dump_result(dump._dump_request_data, prepared_request),
            raw_response = parse_dump_result(dump._dump_response_data, response),
            status = response.status_code,
            **RequestRecord.get_clone_args(self, context)

        # Return fresh copy after update
        return RequestRecord.objects.get(pk =

class RequestRecord(Request):
    A record of a Request that has been sent.
    Contains response and diagnostic information
    about the request.
    raw_request = models.TextField()
    raw_response = models.TextField()
    status = models.PositiveIntegerField(null = True)
    blueprint = models.ForeignKey(
        on_delete = models.SET_NULL,
        null = True)

    def is_success(self):
        return self.status and self.status < 400

    def of_blueprint(blueprint):
        return RequestRecord.objects.filter(
            blueprint = blueprint)

    def get_clone_args(cls, obj, context : dict):
        return dict([
                render_with_context(, context),
                render_with_context(getattr(obj,, context)
            for fld \
            in cls._meta.fields \
            if != \
            and fld in obj._meta.fields \
            and not in [
                   'request', 'id', 'created', 'updated'

## source file continues from here without further TextField examples

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.

Learn More Python

Introduction to Ansible video course logo.

Deploy web apps with the Ansible configuration management tool.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via newsletter, Twitter & Facebook.

Matt Makai 2012-2019