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
