4.4. Models Field Logic
models.BooleanField
- stores a boolean (True
/False
) valueCan be
None
too whennull=True
4.4.1. SetUp
>>> from django.db import models
>>> from django.utils.translation import gettext_lazy as _
4.4.2. Arguments
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.4.3. BooleanField
>>> is_active = models.BooleanField(
... verbose_name=_('Is Active?'),
... null=False,
... blank=False,
... default=True,
... )
4.4.4. 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=100, null=False, blank=False)
... lastname = models.CharField(verbose_name=_('Last Name'), max_length=100, null=False, blank=False, db_index=True)
... is_active = models.BooleanField(verbose_name=_('Is Active'), null=False, blank=False, default=True)
...
... class Meta:
... verbose_name = _('Customer')
... verbose_name_plural = _('Customer')
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
4.4.5. 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 Logic
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `is_active` as `models.BooleanField`:
# - verbose_name=_('Is Active?')
# - null=False
# - blank=False
# - default=True
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `is_active` jako `models.BooleanField`:
# - verbose_name=_('Is Active?')
# - null=False
# - blank=False
# - default=True
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field is_active to person`
# - `Applying demo.0007_person_is_active... 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):
is_active = ...