liveBook · Manning

DateField

As explained in Django in Action

DateField is a Django model field for storing calendar dates in a database column.

DateField stores a calendar date in a Django model and maps that model attribute to a database column. In a musician record, it suits a birth date because the value represents a day, not arbitrary text. section 4.2

Listing 4.2 Musician ORM Model class
# RiffMates/bands/models.py
from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    birth = models.DateField()

A musician model declares DateField alongside CharField attributes, making the database representation visible in ordinary Django model code. section 4.2

Defining the field changes the model description, but the database table changes only after migrations are created and applied. Django tracks the model-to-table change through makemigrations and migrate. section 4.2

Creating a musician with a date

In the Django shell, import date and pass a Python date object through the DateField: Musician.objects.create(first_name="Steve", last_name="Vai", birth=date(1960, 6, 6)). The .create() call inserts the record and returns a Musician object. section 4.4

(venv) RiffMates$ python manage.py shell
Python 3.12.1
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from bands.models import Musician
>>> from datetime import date
>>> steve = Musician.objects.create(first_name="Steve",
 last_name="Vai", birth=date(1960, 6, 6)) #1

The shell example shows the complete value path: construct a Python date, provide it as the birth keyword argument, and let the ORM insert the musician. section 4.4

How Django in Action covers it

How deep it goes
Defines the field, migration workflow, and Python date mapping without descending into database SQL details.
Examples
Uses a Musician model and creates Steve Vai, John Lennon, and John Bonham with date-valued birth fields.
What it assumes
Assumes basic Django model and shell familiarity while demonstrating the date-to-database mapping through a concrete musician model.
What you can do after
Build it from scratch by declaring DateField, applying migrations, and creating records with Python date values.
How it connects
Connects Django ORM models with database columns, migrations, Python date objects, and CharField attributes.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.