4.5. Models Field Date
models.DateTimeField
- stores both date and time, represented in Python by adatetime.datetime
instancemodels.DateField
- stores the date, represented in Python by adatetime.date
instancemodels.TimeField
- stores the time, represented in Python by adatetime.time
instancemodels.DurationField
- stores a period of time, represented in Python by adatetime.timedelta
instance
4.5.1. SetUp
>>> from django.db import models
>>> from django.utils.translation import gettext_lazy as _
4.5.2. Arguments
auto_add
(DateField, DateTimeField)auto_add_now
(DateField, DateTimeField)blank
choices
db_column
db_index
default
editable
error_message
help_text
limit_choices_to
max_length
null
primary_key
unique
validators
verbose_name
4.5.3. DateTimeField
DateTimeField
>>> since = models.DateTimeField(
... verbose_name=_('Since'),
... null=True,
... blank=True,
... default=None,
... )
4.5.4. DateField
DateField
>>> birthdate = models.DateField(
... verbose_name=_('Birthdate'),
... null=True,
... blank=True,
... default=None,
... )
4.5.5. TimeField
TimeField
>>> wakeup = models.TimeField(
... verbose_name=_('Wakeup'),
... null=True,
... blank=True,
... default=None,
... )
4.5.6. DurationField
DurationField
>>> duration = models.DurationField(
... verbose_name=_('Duration'),
... null=True,
... blank=True,
... default=None,
... )
4.5.7. Use Case - 1
>>>
... from django.db import models
... from django.utils.translation import gettext_lazy as _
...
...
... class Customer(models.Model):
... creation_date = models.DateTimeField(verbose_name=_('Creation Date'), auto_now_add=True, editable=False)
... modification_date = models.DateTimeField(verbose_name=_('Modification Date'), auto_now=True)
... birthdate = models.DateField(verbose_name=_('Birthdate'), null=True, blank=True, default=None)
... since = models.TimeField(verbose_name=_('Since'), null=True, blank=True, default=None)
... duration = models.DurationField(verbose_name=_('Duration'), null=True, blank=True, default=None)
...
... class Meta:
... verbose_name = _('Customer')
... verbose_name_plural = _('Customer')
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
4.5.8. Assignments
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Date
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `birthdate` as `models.DateField`:
# - verbose_name=_('Birthdate')
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `birthdate` jako `models.DateField`:
# - verbose_name=_('Birthdate')
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field birthdate to person`
# - `Applying demo.0007_person_birthdate... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
birthdate = ...
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Date
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `wakeup` as `models.TimeField`:
# - verbose_name=_('Wakeup')
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `wakeup` jako `models.TimeField`:
# - verbose_name=_('Wakeup')
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field wakeup to person`
# - `Applying demo.0007_person_wakeup... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
wakeup = ...
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Date
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `since` as `models.DateTimeField`:
# - verbose_name=_('Since')
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `since` jako `models.DateTimeField`:
# - verbose_name=_('Since')
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field since to person`
# - `Applying demo.0007_person_since... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
since = ...
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Date
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `duration` as `models.TimedeltaField`:
# - verbose_name=_('Duration')
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `duration` jako `models.TimedeltaField`:
# - verbose_name=_('Duration')
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field duration to person`
# - `Applying demo.0007_person_duration... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
duration = ...