Full answer
A model describes one kind of record. Django turns each model class into a database table, each field into a column, and each instance into one row.
Common field types:
CharField(max_length=...)for short text,TextFieldfor long textIntegerField,DecimalField(max_digits, decimal_places)for money (neverFloatFieldfor money, because floats round)BooleanField,DateField,DateTimeFieldEmailField,URLField,SlugField(text with extra validation)ForeignKey,OneToOneField,ManyToManyFieldfor relations
Options that confuse people:
null=Trueis about the database. The column can hold NULL.blank=Trueis about validation. Forms andfull_clean()allow an empty value.default=is used when you create an object without that field.unique=Trueadds a unique index.choices=limits allowed values and givesget_FOO_display().
For text fields, the Django convention is to avoid null=True and use blank=True with an empty string, so there is only one "empty" value, not two (NULL and "").
Django adds an auto-increment primary key named id unless you define one. Since Django 3.2 the type comes from DEFAULT_AUTO_FIELD; new projects use BigAutoField, which became the default value in Django 6.0.
Saving with save() does not run validation. Call full_clean() (forms do this for you) to check max_length, choices and validators before saving.
import sys
import types
import django
from django.conf import settings
# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__
settings.configure(
SECRET_KEY="demo-only-key-not-for-production",
ALLOWED_HOSTS=["testserver"], # the host name used by Django's test client
INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "school"],
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()
from decimal import Decimal
from django.core.exceptions import ValidationError
from django.db import models, connection
class FeePayment(models.Model):
MODES = [("cash", "Cash"), ("upi", "UPI"), ("card", "Card")]
student_name = models.CharField(max_length=40)
amount = models.DecimalField(max_digits=8, decimal_places=2)
mode = models.CharField(max_length=4, choices=MODES, default="cash")
remarks = models.CharField(max_length=100, blank=True) # empty string, not NULL
paid_on = models.DateField(null=True, blank=True) # may be unknown
class Meta:
app_label = "school"
with connection.schema_editor() as editor:
editor.create_model(FeePayment)
p = FeePayment.objects.create(student_name="Ravi", amount=Decimal("1250.50"), mode="upi")
print(p.id, p.amount, p.get_mode_display(), repr(p.remarks), p.paid_on)
bad = FeePayment(student_name="Meera", amount=Decimal("10.00"), mode="cheque")
try:
bad.full_clean()
except ValidationError as e:
print(sorted(e.message_dict))
Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): 1 1250.50 UPI '' None ['mode']
Key points
- One model = one table, one field = one column
- null is a database setting, blank is a validation setting
- Use DecimalField for money
- save() does not validate; full_clean() does
- Text fields usually use blank=True without null=True
Common mistakes
- Mixing up null and blank
- Using FloatField for money and then seeing 0.30000000000000004
- Assuming save() checks max_length or choices
- Adding null=True to CharField and ending up with two kinds of empty value
Follow-ups
- What does DEFAULT_AUTO_FIELD control?
- How would you store a field whose value is computed from two others?
- How do you add a validator to a field?
Tips
- Give the null vs blank difference in one sentence each
- Mention full_clean() when talking about validation
- Use a money example to justify DecimalField