4.3. Models Field Numeric
models.IntegerField
- stores a 32-bit value, minimum -2,147,483,648 and maximum 2,147,483,647models.FloatField
- stores a 64-bit floating-point number, minimum -1.79E+308 and maximum 1.79E+308models.DecimalField
- stores a fixed-point number (e.g. a monetary amount), requires two arguments:max_digits
anddecimal_places
models.SmallIntegerField
- stores a 16-bit value, minimum -32,768 and maximum 32,767models.BigIntegerField
- stores a 64-bit value, minimum -9,223,372,036,854,775,808 and maximum 9,223,372,036,854,775,807models.PositiveSmallIntegerField
- stores a positive 16-bit value (unsigned int16), minimum 0 and maximum 65,535models.PositiveIntegerField
- stores a positive 32-bit value (unsigned int32), minimum 0 and maximum 4,294,967,295models.PositiveBigIntegerField
- stores a positive 64-bit value (unsigned int64), minimum 0 and maximum 18,446,744,073,709,551,615models.AutoField
- stores anIntegerField
that automatically increments according to available IDsmodels.BigAutoField
- stores a 64-bit integer, much like anAutoField
except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807models.SmallAutoField
- like an`AutoField
, but only allows values under a certain (database-dependent) pointfloat vs. decimal (IEEE-754)
4.3.1. SetUp
>>> from django.core.validators import MinValueValidator, MaxValueValidator
>>> from django.db import models
>>> from django.utils.translation import gettext_lazy as _
4.3.2. Arguments
decimal_places
(DecimalField)max_digits
(DecimalField)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.3.3. IntegerField
IntegerField
SmallIntegerField
BigIntegerField
>>> height = models.IntegerField(
... verbose_name=_('Height'),
... help_text=_('cm'),
... null=True,
... blank=True,
... default=None,
... )
4.3.4. PositiveIntegerField
PositiveIntegerField
PositiveSmallIntegerField
PositiveBigIntegerField
4.3.5. FloatField
FloatField
>>> weight = models.FloatField(
... verbose_name=_('Weight'),
... help_text=_('kg'),
... null=True,
... blank=True,
... default=None,
... )
4.3.6. DecimalField
DecimalField
>>> salary = models.DecimalField(
... verbose_name=_('Salary'),
... help_text=_('USD per year'),
... max_digits=12,
... decimal_places=2,
... null=True,
... blank=True,
... default=None,
... )
4.3.7. AutoField
AutoField
BigAutoField
SmallAutoField
4.3.8. Validators
MinLengthValidator
MaxLengthValidator
>>> height = models.IntegerField(
... verbose_name=_('Height'),
... help_text=_('cm'),
... validators=[MinValueValidator(150), MaxValueValidator(220)],
... null=True,
... blank=True,
... default=None,
... )
>>> weight = models.FloatField(
... verbose_name=_('Weight'),
... help_text=_('kg'),
... validators=[MinValueValidator(50), MaxValueValidator(150)],
... null=True,
... blank=True,
... default=None,
... )
>>> salary = models.DecimalField(
... verbose_name=_('Salary'),
... help_text=_('USD per year'),
... validators=[MinValueValidator(0.0), MaxValueValidator(999_999.99)],
... max_digits=12,
... decimal_places=2,
... null=True,
... blank=True,
... default=None,
... )
4.3.9. Use Case - 1
>>>
... from django.core.validators import MinValueValidator, MaxValueValidator
... 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)
... height = models.PositiveSmallIntegerField(verbose_name=_('Height'), help_text=_('cm'), null=True, blank=True, default=None, validators=[MinValueValidator(0), MaxValueValidator(250)])
... weight = models.FloatField(verbose_name=_('Weight'), help_text=_('kg'), null=True, blank=True, default=None, validators=[MinValueValidator(0), MaxValueValidator(300)])
... salary = models.DecimalField(verbose_name=_('Salary'), help_text=_('USD per month'), blank=True, null=True, default=None, max_digits=6, decimal_places=2, validators=[MinValueValidator(0), MaxValueValidator(10_000)])
...
... class Meta:
... verbose_name = _('Customer')
... verbose_name_plural = _('Customer')
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
4.3.10. 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 Numeric
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `height` as `models.IntegerField`:
# - verbose_name=_('Height')
# - help_text=_('cm')
# - validator=[MinValueValidator(150), MaxValueValidator(250)]
# - 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 `height` jako `models.IntegerField`:
# - verbose_name=_('Height')
# - help_text=_('cm')
# - validator=[MinValueValidator(150), MaxValueValidator(250)]
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field height to person`
# - `Applying demo.0007_person_height... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
height = ...
# 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 Numeric
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `weight` as `models.FloatField`:
# - verbose_name=_('Weight')
# - help_text=_('kg')
# - validator=[MinValueValidator(50), MaxValueValidator(150)]
# - 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 `weight` jako `models.FloatField`:
# - verbose_name=_('Weight')
# - help_text=_('kg')
# - validator=[MinValueValidator(50), MaxValueValidator(150)]
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field weight to person`
# - `Applying demo.0007_person_weight... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
weight = ...
# 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 Numeric
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `salary` as `models.DecimalField`:
# - verbose_name=_('Salary')
# - help_text=_('USD per year')
# - validators=[MinValueValidator(0.0), MaxValueValidator(999_999.99)]
# - max_digits=12
# - decimal_places=2
# - 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 `salary` jako `models.DecimalField`:
# - verbose_name=_('Salary')
# - help_text=_('USD per year')
# - validators=[MinValueValidator(0.0), MaxValueValidator(999_999.99)]
# - max_digits=12
# - decimal_places=2
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field salary to person`
# - `Applying demo.0007_person_salary... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
salary = ...