Django model field types

Django Model – Field Types | Programspeaker

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 NameDescription
AutoFieldIt is an integer field that automatically increments
BigAutoFieldIt is a 64-bit integer field. Similar to AutoField and guaranteed to fit numbers from 1 to 9223372036854775807
BigIntegerFieldIt is a 64-bit integer field. Similar to AutoField and guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807
BinaryFieldA field to store data as binary(eg:128 bytes)
BooleanFieldThis is a True or False field
CharFieldA field that stores text-based values
DateFieldA field that stores date values. In python, it is represented by a datetime.date instance
DateTimeFieldIt is used to store date and time, represented in Python by a datetime.datetime instance
DecimalFieldIt is a fixed precision decimal number represented in Python by a Decimal instance. max_digits and decimal_places are 2 required arguments
DurationFieldA field for storing periods of time in HH: MM: SS format
EmailFieldIt is a character field that stores values in an email format
FileFieldIt is a file upload field
FloatFieldIt is a floating point number in python represented by a float instance. It has no need of max_digits and decimal_places like DecimalField
ImageFieldImageField is a FileFiled with uploads restricted to image formats only
IntegerFieldIt is an integer field value from -2147483648 to 2147483648
GenericIPAddressFieldStores IPV4 and IPV6 addresses in string format (eg:”0.0.0.0“)
JsonFieldA field of storing JSON encoded data. In Python data is represented in Python native format: dictionaries, lists, strings, numbers, boolean, and None
PositiveIntegerFieldLike an integer field, the value must be either positive or zero and in between(0 to 2147483647).
PositiveSmallIntegerFieldIt is an integer field like a PositiveIntegerField, with values accept from zero to 32767
PositiveBigIntegerFieldLike a PositiveIntegerField, but only values from 0 to 9223372036854775807
SlugFieldA slug is a short label for something, containing only letters, numbers, underscore, or hyphens and they are actually used in URLs.
SmallAutoFieldLike an AutoField. Values from 1 to 32767
SmallIntegerFieldLike an IntegerField. Values from -32768 to 32768
TextFieldA large text field. The default form widget is textarea
TimeFieldStore time in the HH: MM: SS format, represented in Python by a datetime.time instance.
URLFieldURL field is a char field for storing URLs. It generally stores webpage links or any other URLs.
UUIDFieldA 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

FieldsDescription
ForeignKeyMany-to-one relationship. Requires two positional arguments: One is the class to which the model is related, and the second one is on_delete
ManyToManyRelationshipMany to many relationships. . Requires a positional argument: One is the class to which the model is related.
OneToOneFieldWhich is similar to ForeignKey with unique=True

To know more about Django model field types please go through the official documentation

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top