Examples of numeric fields are given:
AutoField
An auto-incrementing integer generally used for primary keys.
from django.db import models
class MyModel(models.Model):
pk = models.AutoField()
Each model gets a primary key field (called
id
) by default. Therefore, it is not necessary to duplicate an id field in the model for the purposes of a primary key.
BigIntegerField
An integer fitting numbers from -9223372036854775808
to 9223372036854775807
(8 Bytes
).
from django.db import models
class MyModel(models.Model):
number_of_seconds = models.BigIntegerField()
IntegerField
The IntegerField is used to store integer values from -2147483648 to 2147483647 (4 Bytes
).
from django.db import models
class Food(models.Model):
name = models.CharField(max_length=255)
calorie = models.IntegerField(default=0)
default
parameter is not mandatory. But it's useful to set a default value.
PositiveIntegerField
Like an IntegerField, but must be either positive or zero (0). The PositiveIntegerField is used to store integer values from 0 to 2147483647 (4 Bytes
). This can be useful at field which should be semantically positive. For example if you are recording foods with its calories, it should not be negative. This field will prevent negative values via its validations.
from django.db import models
class Food(models.Model):
name = models.CharField(max_length=255)
calorie = models.PositiveIntegerField(default=0)
default
parameter is not mandatory. But it's useful to set a default value.
SmallIntegerField
The SmallIntegerField is used to store integer values from -32768 to 32767 (2 Bytes
).
This field is useful for values not are not extremes.
from django.db import models
class Place(models.Model):
name = models.CharField(max_length=255)
temperature = models.SmallIntegerField(null=True)
PositiveSmallIntegerField
The SmallIntegerField is used to store integer values from 0to 32767 (2 Bytes
).
Just like SmallIntegerField this field is useful for values not going so high and should be semantically positive. For example it can store age which cannot be negative.
from django.db import models
class Staff(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
age = models.PositiveSmallIntegerField(null=True)
Besides PositiveSmallIntegerField is useful for choices, this is the Djangoic way of implementing Enum:
from django.db import models
from django.utils.translation import gettext as _
APPLICATION_NEW = 1
APPLICATION_RECEIVED = 2
APPLICATION_APPROVED = 3
APPLICATION_REJECTED = 4
APLICATION_CHOICES = (
(APPLICATION_NEW, _('New')),
(APPLICATION_RECEIVED, _('Received')),
(APPLICATION_APPROVED, _('Approved')),
(APPLICATION_REJECTED, _('Rejected')),
)
class JobApplication(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
status = models.PositiveSmallIntegerField(
choices=APLICATION_CHOICES,
default=APPLICATION_NEW
)
...
Definition of the choices as class variables or module variables according to the situation is a good way to use them. If choices are passed to field without friendly names than it will create confusion.
DecimalField
A fixed-precision decimal number, represented in Python by a Decimal instance. Unlike IntegerField and its derivatives this field has 2 required arguments:
If you want to store numbers up to 99 with 3 decimal places you need use max_digits=5
and decimal_places=3
:
class Place(models.Model):
name = models.CharField(max_length=255)
atmospheric_pressure = models.DecimalField(max_digits=5, decimal_places=3)