4.1. Models About
What are model fields?
Model field arguments
Fat model architecture
Single File vs. Models per file
>>>
... from django.db import models
...
...
... class Customer(models.Model):
... firstname = models.CharField(max_length=50)
... lastname = models.CharField(max_length=50)
... birthdate = models.DateField()
... email = models.EmailField(max_length=100)
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
4.1.1. Attributes
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.1.2. Use Case - 1
>>>
... from django.db import models
... from django.utils.translation import gettext_lazy as _
...
...
... class Customer(models.Model):
... firstname = models.CharField(verbose_name=_('First Name'), max_length=50)
... lastname = models.CharField(verbose_name=_('Last Name'), max_length=50, db_index=True)
... birthdate = models.DateField(verbose_name=_('Birthdate'), null=True, blank=True, default=None)
... email = models.EmailField(verbose_name=_('Email'), max_length=100, null=True, blank=True, default=None, unique=True)
... phone = models.CharField(verbose_name=_('Phone Number'), max_length=20, null=True, blank=True, default=None)
... tax_number = models.CharField(verbose_name=_('Tax Number'), max_length=20, null=True, blank=True, default=None)
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
...
... class Meta:
... app_label = 'shop'
... verbose_name = _('Customer')
... verbose_name_plural = _('Customers')
4.1.3. 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 About
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% English
# 0. Use `myproject.demo`
# 1. Create model `Group`
# 2. Add field `name` as `models.CharField`
# - verbose_name=_('Name')
# - max_length=30
# - null=False
# - blank=False
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Stwórz model `Group`
# 2. Dodaj pole `name` jako `models.CharField`
# - verbose_name=_('Name')
# - max_length=30
# - null=False
# - blank=False
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Create model Group`
# - `Applying demo.0001_initial... 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 _
...
# 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 About
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Group`
# 2. Add method `__str__` which returns `name`
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Group`
# 2. Dodaj metodę `__str__` która zwraca `name`
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `No changes detected`
# - `No migrations to apply.`
# %% Hints
# - `Model.__str__()`
# %% 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 Group(models.Model):
name = models.CharField(verbose_name=_('Group'), max_length=30, null=False, blank=False)
# 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 About
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Group`
# 2. Add class `Meta` with:
# - app_label = 'demo'
# - verbose_name = _('Group')
# - verbose_name_plural = _('Groups')
# - ordering = ['name']
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Group`
# 2. Dodaj klasę `Meta` z:
# - app_label = 'demo'
# - verbose_name = _('Group')
# - verbose_name_plural = _('Groups')
# - ordering = ['name']
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Create model Person`
# - `Applying demo.0003_create_person... OK`
# %% Hints
# - `class Meta`
# %% 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 Group(models.Model):
name = models.CharField(verbose_name=_('Group'), max_length=30, null=False, blank=False)
def __str__(self):
return f'{self.name}'