5.3. Models Field Numeric

  • models.IntegerField - stores a 32-bit value, minimum -2,147,483,648 and maximum 2,147,483,647

  • models.FloatField - stores a 64-bit floating-point number, minimum -1.79E+308 and maximum 1.79E+308

  • models.DecimalField - stores a fixed-point number (e.g. a monetary amount), requires two arguments: max_digits and decimal_places

  • models.SmallIntegerField - stores a 16-bit value, minimum -32,768 and maximum 32,767

  • models.BigIntegerField - stores a 64-bit value, minimum -9,223,372,036,854,775,808 and maximum 9,223,372,036,854,775,807

  • models.PositiveSmallIntegerField - stores a positive 16-bit value (unsigned int16), minimum 0 and maximum 65,535

  • models.PositiveIntegerField - stores a positive 32-bit value (unsigned int32), minimum 0 and maximum 4,294,967,295

  • models.PositiveBigIntegerField - stores a positive 64-bit value (unsigned int64), minimum 0 and maximum 18,446,744,073,709,551,615

  • models.AutoField - stores an IntegerField that automatically increments according to available IDs

  • models.BigAutoField - stores a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807

  • models.SmallAutoField - like an `AutoField, but only allows values under a certain (database-dependent) point

  • float vs decimal (IEEE-754)

>>> 
... 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}'

5.3.1. 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