Model Fields (Django)

Model fields are the typed attributes on a Django model that map to database columns, like CharField, IntegerField, BooleanField, and DateTimeField. Each field type sets the column's data type, validation, and form widget.

Django

Field types do triple duty: they define the database column, the default form field for that data, and validation rules. CharField needs max_length; TextField is for long text; IntegerField, FloatField, DecimalField for numbers; BooleanField; DateField/DateTimeField; EmailField and URLField add format validation.

Common options apply across field types: null (allows NULL in the database), blank (allows empty in forms — different from null), default, unique, choices (a fixed set of values), and db_index. Relationship fields ForeignKey, ManyToManyField, and OneToOneField link models together.

A frequent gotcha is null vs blank: null is database-level, blank is validation-level. For text fields prefer blank=True and avoid null=True so empty strings are stored consistently.

Syntax

name = models.CharField(max_length=100, blank=True)

Examples

Python

class Product(models.Model):
    name = models.CharField(max_length=120)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    in_stock = models.BooleanField(default=True)
    STATUS = [("d", "Draft"), ("p", "Published")]
    status = models.CharField(max_length=1, choices=STATUS, default="d")

Learn it in a lesson

See also

Back to the Dictionary