Fields are a crucial part of a relational database storing data. Each field determines a data type that can store a specific type of data. Django provides several field types. A field type is written in PascalCase. Some field types require specific arguments. For example, max_length is required for CharField. In this section, we'll explain key data field types.
This field type is one of the most frequently used field types. This field can store small-to-large strings.
max_length
: Required argument. Without this argument, Django gives an error message. Using this argument, you can specify the maximum length of the field.This field is used to store a large text.
max_length
: Optional argument. Using this argument, you can specify the maximum length of the field.This field is used to store strings of alphabets, numbers, underscores, or hyphens, which are generally used in URLs.
max_length
: Optional argument. If you don't specify max_length
, a default of 50
is used.This field is used to store a URL validated by a URL validator.
max_length
: Optional argument. If you don't specify max_length
, a default of 200
is used.This field is used to store a True
or False
value. In a form input, this field usually becomes a check box input.
This field is used to store an integer. According to the Django documentation, Values from -2147483648
to 2147483647
are safe in all databases supported by Django.
This field is used to store a positive integer or zero value. According to the Django documentation, Values from 0
to 2147483647
are safe in all databases supported by Django.
This field is used to store a floating-point number represented in Python by a float instance.
This field is used to store a fixed-precision decimal number, represented in Python by a decimal instance.
decimal_places
: Required argument. Using this argument, you can specify the number of decimal places to store the number.max_digits
: Required argument. Using this argument, you can specify the maximum number of digits allowed in the number. This number must be greater than or equal to decimal_places
.Floating-point numbers in FloatField are handled in binary numbers. In some cases, calculations using this field may not be very accurate, but the data processing speed tends to be faster.
Decimal numbers in DecimalField are handled in decimal numbers. Calculations using this field are more accurate, but the data processing speed tends to be slower.
This field is used to store an integer that automatically increments. This type of field is used for primary key IDs. Django gives each model an auto-incrementing primary key by default, so you usually won’t need to use this field type.
This field is used to store a date, represented in Python by a datetime.date
instance.
auto_now_add
: Optional argument. This option is often used for the created_at
column in a database table. When this argument is True, the value in the field is set with the current date when the record object is first created.auto_now
: Optional argument. This option is often used for the updated_at
column in a database table. When this argument is True, the value in the field is updated with the current date when the record object is updated and saved.This field is used to store date and time, represented in Python by a datetime.datetime
instance.
The auto_now_add
and auto_now
options are optional arguments for this field type.
This field is used to store time, represented in Python by a datetime.time
instance.
The auto_now_add
and auto_now
options are optional arguments for this field type.
When auto_now_add
or auto_now
argument is True, you won't be able to modify the field (DateField
or DateTimeField
) as it is used for the auto-generated timestamp. If you want to modify the field, you need to try another implementation using imported modules like the ones below.
default=date.today
- from datetime.date.today()
default=timezone.now
- from django.utils.timezone.now()
We'll explain this in the later section of this chapter.
This field is used to store image file information. The Pillow library is required to use this field type.
upload_to
: Required argument. Using this argument, you can specify the location (subdirectory) to save an uploaded image file. This argument only specifies a subdirectory path. The main directory for uploading files is defined in the settings.py file – MEDIA_ROOT
and MEDIA_URL
. If you are not creating a subdirectory, you can use a blank for the option value (upload_to=' '
). We'll explain how to edit settings.py for media file handling later.height_field
: Optional argument. Using this argument, you can specify the height of the image.width_field
: Optional argument. Using this argument, you can specify the width of the image.Pillow is a Python Imaging Library (PIL) fork that adds image processing capabilities to the Python interpreter. When you use ImageField
in Django, you need this library in your app.
This field is used to store file information. Using this field type, you can handle other-than-image files.
There are optional arguments for this field type, such as upload_to
and storage
.
Fields are a crucial part of a relational database storing data. Each field determines a data type that can store a specific type of data. Django provides several field types. A field type is written in PascalCase. Some field types require specific arguments. For example, max_length is required for CharField. In this section, we'll explain key data field types.
This field type is one of the most frequently used field types. This field can store small-to-large strings.
max_length
: Required argument. Without this argument, Django gives an error message. Using this argument, you can specify the maximum length of the field.This field is used to store a large text.
max_length
: Optional argument. Using this argument, you can specify the maximum length of the field.This field is used to store strings of alphabets, numbers, underscores, or hyphens, which are generally used in URLs.
max_length
: Optional argument. If you don't specify max_length
, a default of 50
is used.This field is used to store a URL validated by a URL validator.
max_length
: Optional argument. If you don't specify max_length
, a default of 200
is used.This field is used to store a True
or False
value. In a form input, this field usually becomes a check box input.
This field is used to store an integer. According to the Django documentation, Values from -2147483648
to 2147483647
are safe in all databases supported by Django.
This field is used to store a positive integer or zero value. According to the Django documentation, Values from 0
to 2147483647
are safe in all databases supported by Django.
This field is used to store a floating-point number represented in Python by a float instance.
This field is used to store a fixed-precision decimal number, represented in Python by a decimal instance.
decimal_places
: Required argument. Using this argument, you can specify the number of decimal places to store the number.max_digits
: Required argument. Using this argument, you can specify the maximum number of digits allowed in the number. This number must be greater than or equal to decimal_places
.Floating-point numbers in FloatField are handled in binary numbers. In some cases, calculations using this field may not be very accurate, but the data processing speed tends to be faster.
Decimal numbers in DecimalField are handled in decimal numbers. Calculations using this field are more accurate, but the data processing speed tends to be slower.
This field is used to store an integer that automatically increments. This type of field is used for primary key IDs. Django gives each model an auto-incrementing primary key by default, so you usually won’t need to use this field type.
This field is used to store a date, represented in Python by a datetime.date
instance.
auto_now_add
: Optional argument. This option is often used for the created_at
column in a database table. When this argument is True, the value in the field is set with the current date when the record object is first created.auto_now
: Optional argument. This option is often used for the updated_at
column in a database table. When this argument is True, the value in the field is updated with the current date when the record object is updated and saved.This field is used to store date and time, represented in Python by a datetime.datetime
instance.
The auto_now_add
and auto_now
options are optional arguments for this field type.
This field is used to store time, represented in Python by a datetime.time
instance.
The auto_now_add
and auto_now
options are optional arguments for this field type.
When auto_now_add
or auto_now
argument is True, you won't be able to modify the field (DateField
or DateTimeField
) as it is used for the auto-generated timestamp. If you want to modify the field, you need to try another implementation using imported modules like the ones below.
default=date.today
- from datetime.date.today()
default=timezone.now
- from django.utils.timezone.now()
We'll explain this in the later section of this chapter.
This field is used to store image file information. The Pillow library is required to use this field type.
upload_to
: Required argument. Using this argument, you can specify the location (subdirectory) to save an uploaded image file. This argument only specifies a subdirectory path. The main directory for uploading files is defined in the settings.py file – MEDIA_ROOT
and MEDIA_URL
. If you are not creating a subdirectory, you can use a blank for the option value (upload_to=' '
). We'll explain how to edit settings.py for media file handling later.height_field
: Optional argument. Using this argument, you can specify the height of the image.width_field
: Optional argument. Using this argument, you can specify the width of the image.Pillow is a Python Imaging Library (PIL) fork that adds image processing capabilities to the Python interpreter. When you use ImageField
in Django, you need this library in your app.
This field is used to store file information. Using this field type, you can handle other-than-image files.
There are optional arguments for this field type, such as upload_to
and storage
.