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.
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