Django is an open-source Python web framework. It makes the web development process fast and straightforward through its collection of modules. 

Since its initial release in 2005, the framework has come a long way. With every new update, it is getting more and more robust. 

Let’s discover what new features and updates it brings in Django 5.0.

 

Significant Updates in Django 5.0

Released on 4 December 2023, Django 5.0 introduces numerous updates to enhance the web development experience. 

Some of the primary improvements in Django are as per below:

 

Straightforward Rendering of Form Fields

One of the notable improvements you can notice in Django 5.0 is that Form Fields are easy to render now.

Form fields in Django have numerous elements, such as descriptive labels, help text, error labels, etc. 

It was always tiresome to lay out all manually. Thankfully in this new version, you don’t need to bother about it.

Django 5 features field group templates. These templates simplify the rendering of all form field components, such as widgets, help text, labels, errors, and more. 

Earlier:

 

 <form>
   ...
   <div>
     {{ form.name.label_tag }}
     {% if form.name.help_text %}
       <div class="helptext" id="{{ form.name.auto_id }}_helptext">
         {{ form.name.help_text|safe }}
       </div>
     {% endif %}
     {{ form.name.errors }}
     {{ form.name }}
     <div class="row">
       <div class="col">
         {{ form.email.label_tag }}
         {% if form.email.help_text %}
           <div class="helptext" id="{{ form.email.auto_id }}_helptext">
             {{ form.email.help_text|safe }}
           </div>
         {% endif %}
         {{ form.email.errors }}
         {{ form.email }}
       </div>
       <div class="col">
         {{ form.password.label_tag }}
         {% if form.password.help_text %}
           <div class="helptext" id="{{ form.password.auto_id }}_helptext">
             {{ form.password.help_text|safe }}
           </div>
         {% endif %}
         {{ form.password.errors }}
         {{ form.password }}
       </div>
     </div>
   </div>
   ...
 </form>

Now:

 <form>
   ...
   <div>
     {{ form.name.as_field_group }}
     <div class="row">
       <div class="col">{{ form.email.as_field_group }}</div>
       <div class="col">{{ form.password.as_field_group }}</div>
     </div>
   </div>
   ...
 </form>

 

Database Generated Model Field

The database-generated model field is another prominent update you can notice in Django 5.0.  

The latest GeneratedField in Django lets users create database-generated columns. The good thing is that all database backends support it.

It is going to be beneficial for fields computed from other fields.

For example:

from django.db import models
from django.db.models import F

class Square(models.Model):
    side = models.IntegerField()
    area = models.GeneratedField(
        expression=F("side") * F("side"),
        output_field=models.BigIntegerField(),
        db_persist=True,
    )

 

This function can significantly improve the efficiency of the database. Moreover, it maintains the integrity of data.

Python Compatibility

Django is keeping pace with the ever-evolving Python language. With Django 5.0, users can relish the latest Python features and improvements. This new version supports Python 3.10, 3.11, and 3.12. 

Not only does it ensure the best performance but also improves security. Now developers can relish the full potential of Django 5.0.

 

Facet Filters in the Admin

The Django 5.0 comes with facet counts for applied filters on the admin change list. 

Developers can toggle this feature using UI (User Interface). It improves the admin interface by presenting facet counts alongside filters. 

Users can now get a quick insight into the distribution of data.

 

Write Field Choice Easily

In the earlier version of Django, it was challenging to list field choices. Users had to make an inconvenient arrangement of 2-tuples or Enumeration subclasses to list the choices available to Field.choices and ChoiceField.choices objects. 

See the following example:

HQ_LOCATIONS = [
    ("United States", [("nyc", "New York"), ("la", "Los Angeles")]),
    ("Japan", [("tokyo", "Tokyo"), ("osaka", "Osaka")]),
    ("virtual", "Anywhere"),
]

Nevertheless, this latest version lets you use concise declarations with the help of dictionary mappings:

HQ_LOCATIONS = {
    "United States": {"nyc": "New York", "la": "Los Angeles"},
    "Japan": {"tokyo": "Tokyo", "osaka": "Osaka"},
    "virtual": "Anywhere",
}

 

It simplifies choices to encode as literals. 

AsyncClient

Django 5.0 features additional asynchronous methods to the Client as well as AsyncClient. It supports asynchronous testing of Django applications. Users can now create tests that replicate the asynchronous behavior of the application. 

 

Database-Computed Default Values

Django 5.0 lets you define database-computed default values. It means you get more powerful and accurate default settings. 

The new `Field.db_default` parameter enables users to set database-computed default values for model fields quickly.

It is specifically helpful for time stamps or calculated fields. Although it is a minor change, it will have a substantial impact on the integrity of your data. Users can define default values using database functions.

 

Features Deprecated in 5.0

Django 5.0 also has abolished a few old features. Therefore, you must check whether your code relies on any of them. If yes, you will need to update it accordingly. 

These features were depreciated in previous versions. 

Some notable ones include:

 

Conclusion

Django 5.0 introduces numerous updates and features that take the web development game to the next level. The platform has solidified its position as a powerful and versatile web framework

It has turned into a crucial tool for building websites and web applications. Enhanced flexibility in declaring field choices, improved performance, and numerous security features make it one of the best Python web frameworks. 

I’ve been working with Django since version 0.96 (2007), so if you need help with it,

Contact Now

Leave details and I will get back to you