In this tutorial, we will understand the Django fields and field types.
The field defines in the models.py is the column names of the mapped table. The field name should not be a python reserved keyword like save, delete, etc. Django provides various built-in field types, they are;
You may like: JWT Authentication – Django REST Framework.
Field Name | Description |
AutoField | It is an integer field that automatically increments |
BigAutoField | It is a 64-bit integer field. Similar to AutoField and guaranteed to fit numbers from 1 to 9223372036854775807 |
BigIntegerField | It is a 64-bit integer field. Similar to AutoField and guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807 |
BinaryField | A field to store data as binary(eg:128 bytes) |
BooleanField | This is a True or False field |
CharField | A field that stores text-based values |
DateField | A field that stores date values. In python, it is represented by a datetime.date instance |
DateTimeField | It is used to store date and time, represented in Python by a datetime.datetime instance |
DecimalField | It is a fixed precision decimal number represented in Python by a Decimal instance. max_digits and decimal_places are 2 required arguments |
DurationField | A field for storing periods of time in HH: MM: SS format |
EmailField | It is a character field that stores values in an email format |
FileField | It is a file upload field |
FloatField | It is a floating point number in python represented by a float instance. It has no need of max_digits and decimal_places like DecimalField |
ImageField | ImageField is a FileFiled with uploads restricted to image formats only |
IntegerField | It is an integer field value from -2147483648 to 2147483648 |
GenericIPAddressField | Stores IPV4 and IPV6 addresses in string format (eg:”0.0.0.0 “) |
JsonField | A field of storing JSON encoded data. In Python data is represented in Python native format: dictionaries, lists, strings, numbers, boolean, and None |
PositiveIntegerField | Like an integer field, the value must be either positive or zero and in between(0 to 2147483647). |
PositiveSmallIntegerField | It is an integer field like a PositiveIntegerField, with values accept from zero to 32767 |
PositiveBigIntegerField | Like a PositiveIntegerField, but only values from 0 to 9223372036854775807 |
SlugField | A slug is a short label for something, containing only letters, numbers, underscore, or hyphens and they are actually used in URLs. |
SmallAutoField | Like an AutoField. Values from 1 to 32767 |
SmallIntegerField | Like an IntegerField. Values from -32768 to 32768 |
TextField | A large text field. The default form widget is textarea |
TimeField | Store time in the HH: MM: SS format, represented in Python by a datetime.time instance. |
URLField | URL field is a char field for storing URLs. It generally stores webpage links or any other URLs. |
UUIDField | A field of storing universally unique identifiers. Which is a good alternative for AutoField and primary_key |
You may like: Date Master – PyPi – ProgramSpeaker
Relationship Fields
Fields | Description |
ForeignKey | Many-to-one relationship. Requires two positional arguments: One is the class to which the model is related, and the second one is on_delete |
ManyToManyRelationship | Many to many relationships. . Requires a positional argument: One is the class to which the model is related. |
OneToOneField | Which is similar to ForeignKey with unique=True |
To know more about Django model field types please go through the official documentation