← Back to Chapters

Common Field Types - Django Models

? Common Field Types - Django Models

? Quick Overview

Django models provide a variety of field types that you can use to define the structure of your database tables. Each field type corresponds to a different kind of data that you want to store, such as text, numbers, dates, or files.

? Key Concepts

  • Each model field maps to a database column
  • Different field types store different data formats
  • Fields control validation and storage behavior

? Syntax / Theory

Django fields are defined as class attributes inside a model class that inherits from models.Model.

? CharField

Used to store short text values and requires max_length.

? View Code Example
# CharField example for short text
from django.db import models
class Product(models.Model):
    name = models.CharField(max_length=100)

? IntegerField

Stores whole numbers such as price or quantity.

? View Code Example
# IntegerField for numeric values
class Product(models.Model):
    price = models.IntegerField()

? FloatField

Stores decimal numbers like ratings.

? View Code Example
# FloatField for decimal values
class Product(models.Model):
    rating = models.FloatField()

? DateField

Stores date values.

? View Code Example
# DateField for storing dates
class Event(models.Model):
    date = models.DateField()

? BooleanField

Stores True/False values.

? View Code Example
# BooleanField for flags
class Product(models.Model):
    is_active = models.BooleanField(default=True)

? TextField

Stores long text without length restriction.

? View Code Example
# TextField for long descriptions
class Product(models.Model):
    description = models.TextField()

? EmailField

Validates and stores email addresses.

? View Code Example
# EmailField with built-in validation
class Customer(models.Model):
    email = models.EmailField()

? Interactive Example

Model Design Insight: Choose fields carefully based on data type and validation needs.

?️ Live Model Simulator

See how Django field constraints work in real-time.

 
 

? Database Preview (Object)

Start typing to see the model instance...

✅ Tips & Best Practices

  • Use CharField for short text and TextField for long content
  • Always define max_length for CharField
  • Use BooleanField for flags
  • EmailField helps enforce correct email formats

? Try It Yourself

  • Create a model with CharField, IntegerField, and TextField
  • Test EmailField validation
  • Add BooleanField flags like is_active