Chapter 3. Django Models and Database

Django Models – Data Field Type

Django Models – Data Field Type
Tag:

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.

Field types for strings

CharField

This field type is one of the most frequently used field types. This field can store small-to-large strings.

Key Arguments
  • max_length: Required argument. Without this argument, Django gives an error message. Using this argument, you can specify the maximum length of the field.

TextField

This field is used to store a large text.

Key Arguments
  • max_length: Optional argument. Using this argument, you can specify the maximum length of the field.

SlugField

This field is used to store strings of alphabets, numbers, underscores, or hyphens, which are generally used in URLs.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 50 is used.

URLField

This field is used to store a URL validated by a URL validator.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 200 is used.

Field types for Logic

BooleanField

This field is used to store a True or False value. In a form input, this field usually becomes a check box input.

3. Field types for Numbers

IntegerField

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.

PositiveIntegerField

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.

FloatField

This field is used to store a floating-point number represented in Python by a float instance.

DecimalField

This field is used to store a fixed-precision decimal number, represented in Python by a decimal instance.

Key Arguments
  • 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.

IdeaNote: Floating-point number vs. decimal number

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.

AutoField

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.

4. Field types for Date & Time

DateField

This field is used to store a date, represented in Python by a datetime.date instance.

Key Arguments
  • 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.

DateTimeField

This field is used to store date and time, represented in Python by a datetime.datetime instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

TimeField

This field is used to store time, represented in Python by a datetime.time instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

IdeaTips: Editable timestamp

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.

  • For DateField: default=date.today - from datetime.date.today()
  • For DateTimeField: default=timezone.now - from django.utils.timezone.now()

We'll explain this in the later section of this chapter.

5. Field types for Files

ImageField

This field is used to store image file information. The Pillow library is required to use this field type.

Key Arguments
  • 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.

IdeaNote: Pillow

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.

FileField

This field is used to store file information. Using this field type, you can handle other-than-image files.

Key Arguments

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.

Field types for strings

CharField

This field type is one of the most frequently used field types. This field can store small-to-large strings.

Key Arguments
  • max_length: Required argument. Without this argument, Django gives an error message. Using this argument, you can specify the maximum length of the field.

TextField

This field is used to store a large text.

Key Arguments
  • max_length: Optional argument. Using this argument, you can specify the maximum length of the field.

SlugField

This field is used to store strings of alphabets, numbers, underscores, or hyphens, which are generally used in URLs.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 50 is used.

URLField

This field is used to store a URL validated by a URL validator.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 200 is used.

Field types for Logic

BooleanField

This field is used to store a True or False value. In a form input, this field usually becomes a check box input.

3. Field types for Numbers

IntegerField

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.

PositiveIntegerField

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.

FloatField

This field is used to store a floating-point number represented in Python by a float instance.

DecimalField

This field is used to store a fixed-precision decimal number, represented in Python by a decimal instance.

Key Arguments
  • 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.

IdeaNote: Floating-point number vs. decimal number

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.

AutoField

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.

4. Field types for Date & Time

DateField

This field is used to store a date, represented in Python by a datetime.date instance.

Key Arguments
  • 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.

DateTimeField

This field is used to store date and time, represented in Python by a datetime.datetime instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

TimeField

This field is used to store time, represented in Python by a datetime.time instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

IdeaTips: Editable timestamp

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.

  • For DateField: default=date.today - from datetime.date.today()
  • For DateTimeField: default=timezone.now - from django.utils.timezone.now()

We'll explain this in the later section of this chapter.

5. Field types for Files

ImageField

This field is used to store image file information. The Pillow library is required to use this field type.

Key Arguments
  • 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.

IdeaNote: Pillow

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.

FileField

This field is used to store file information. Using this field type, you can handle other-than-image files.

Key Arguments

There are optional arguments for this field type, such as upload_to and storage.

Tag: