Django 0 : Database-Generated Columns with “GeneratedField”

The new “GeneratedField” is always computed based on the model’s other fields. It uses the GENERATED ALWAYS SQL syntax. The database itself manages and updates the “GeneratedField” class. Furthermore, it also allows developers to create database-generated columns.

The “GeneratedField” delivers two types of generated columns, including stored and virtual. The value of a stored generated column is stored in the database and calculated when data is written, added, or updated. On the other hand, the value of a virtual generated column does not require any storage and is calculated when the data is read.

The three primary attributes of the “GeneratedField” class includes:

  • GeneratedField.expression: The database uses the “expression” attribute to automatically set the “GeneratedField” value during every model modification. However, the “GeneratedField.expression” should be deterministic and refer to fields from within the model that reside within the database table.
  • GeneratedField.output_field: “output_field” refers to a model field instance that defines the GeneratedField’s data type.
  • GeneratedField.db_persist: The “db_persist” attribute determines whether the column should occupy database storage (like a stored database column) or not occupy database storage (like a virtual database column).

Simple Area Calculation from Dimensions:

Python3




from django.db import models
from django.db.models import F
  
class Square(models.Model):
    side = models.PositiveIntegerField()
    area = models.GeneratedField(
        expression=F("side") * F("side"),
        output_field=models.IntegerField(),
        db_persist=True)


More Complex discount calculation:

Python3




from django.db import models
from django.db.models import F, Func
  
class Product(models.Model):
    price = models.DecimalField(max_digits=10, decimal_places=2)
    discount_rate = models.DecimalField(max_digits=2, decimal_places=2)
    final_price = models.GeneratedField(
        expression=F("price") * (1 - Func('greatest', F("discount_rate"), 0.1)),
        output_field=models.DecimalField(max_digits=10, decimal_places=2),
        db_persist=True
    )


Django 5.0: Significant Features for Web Development in 2024

Django released its latest major version, Django 5.0, on December 4, 2023. This version also offers new features, deprecations, updated functionalities, and more that Django developers can utilize to enhance web application development processes. For a better understanding, let us delve deeper into this version’s core and minor features. In this article we discuss the Django 5.0: Significant Features for Web Development in 2024 those are following.

Django 5.0

Table of Content

  • Django 5.0: Significant Features for Web Development in 2024
  • Managing Admin Changelist Facet Counts
  • Form Field Rendering Enhancements
  • Simplifies Database-Computed Values
  • Database-Generated Columns with “GeneratedField”
  • Flexible Model and Form Field Choices
  • Minor Features Overview
  • Django 5.0 Syntax Updates
  • Conclusion

Similar Reads

Django 5.0 Facets: Managing Admin Changelist Facet Counts

Django, a high-level Python Web Framework, empowers Developers to build secure and scalable web applications easily. Its elegant syntax, rapid development tools, and vast ecosystem of readily available Django packages make it a popular choice for web development projects of all sizes and complexities. Furthermore, an active developer community continuously improves its ecosystem through major and minor version updates for optimized web application development. Minor versions deliver bug updates, adjustments, and deprecations, while major versions unveil new features, improved functionality, and bug updates, adjustments, and deprecations. Django 5.0 recently introduced the concept of Facets. Facets refer to numerical indicators showing the number of results that match each filter....

Django 5.0 : Form Field Rendering Enhancements

...

Django 5.0: Simplifies Database-Computed Values

Django 5.0 introduced field group and field group templates. These concepts enable developers to render different parts of a form field easily, like errors, help texts, labels, and widgets. Developers can access each form field “{{ form.name_of_field }}” in templates in Django forms. Each field comprises an “as_field_group()” function that renders the various parts of the form field. Therefore, the “as_field_group()” function helps developers create generic templates for organizing field elements in the preferred/required layout....

Django 5.0 : Database-Generated Columns with “GeneratedField”

...

Django 5.0: Flexible Model and Form Field Choices

...

Django 5.0: Minor Features Overview

...

Django 5.0 Syntax Updates

...

Conclusion

The newly introduced Field.db_default parameter enables a Django developer to set a database-computed value for a particular field. The value can be a simple one or a database function like “Now”. For example, a “DateTimeField” can use “Now()” as its default value....