Voltage

Voltage

Jan. 3, 2024, 8:23 p.m.

Welcome to Django 5

Back in 2023, Python programmers and developers utilized Django version 4 upto 4th December 2023 when Django version 5 was released. It was a rush to talk about the new features that come shipped with Django 5. However, it is now the right time to analyze these changes and know what we have.

Upgrading Django version.

Upgrading a Django verrsion from a previous version to the latest version may be done in a variety of ways. The first technique is by using pip which is a reliable python package manager then supplying the command with the latest specific version you would like to upgrade to. Assuming you want to upgrade from version 4.0.0 to 5.0.0, we could simply execute the command:

pip install django==5.0.0

On the contrary, you may fail to know the exact latest Django version. In this scenario, just type the installation command without specifying the exact version of Django. You can do this as below.

pip install django

What is new in Django 5?

- Database-computed default values

With the latest release, we can now define default values directly in the database not as developers were forced to do it initially. Assuming a database model below, we can define and compute values as below.

from django.db import models
class Book(models.Model):
    author = models.ForeignKey(...)
    published_year = models.PositiveIntegerField(
        default=models.F('author__birth_year') + 18
    )

 

 In the code above, we compute the book year of publish by simply adding 18 years to the author's year of birth. Initially, we would write the calculation logic on the views called while saving the book or write a signals code that is called when saving the book to the database. This future will be a life saver this year.

- Generated model fields

Django 5 also came with new capabilities where we can now create columns on the target database then automatically map the columns to Django model fields.

from django.db import models
class SensorReading(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    temperature = models.FloatField(generated="AVG(raw_data)")
    class Meta:
        managed = False  # Don't create a table for SensorReading

 

From the code above, the snippet AVG(raw_data) empowers Django to dynamically compute the value for this field directly in the database rather than relying on Python code. It achieves this using the generated keyword flag 'generated' indicating that its value will be calculated by the database.

The value of the flag AVG(raw_data) string specifies the database expression to be used for calculation.

- Simplified form rendering.

In Django 5.0, we can now use field groups to easily manage the layout of form elements fo instance the labels, widgets and the errors.

from django.forms import FieldGroup

class MyForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['name'].field_group = FieldGroup(label="Personal Details")
        self.fields['email'].field_group = FieldGroup(label="Contact Information")

There are series of new features that comes bundled with Django 5. You can easily check out the exhausted documentation from the Django official documentation https://docs.djangoproject.com/en/5.0/releases/5.0/

Is it worth it migrating to Django 5?

Django 5 is the lastest version of Django and was officially rolled out from Beta Stage. This means it is the recommended version of Django to use which has security updates, patches and is one of the stable Django versions.

On the other hand, be cautious while upgrading your project to the latest version of Django to avoid breaking certain functionalities of your project. Backup your code before upgrading to avoid frustrations when something fails. With that, you can easily roll back to the previous version of your application.

 

 

Comments