Django 5.0 Release - Highlights & Free Sample

Django 5.0 Release - Highlights & Free Sample

Django 5.0 release is out - this article highlights the latest changes and also provides a free sample to play with.

ยท

4 min read

Hello Coders!

As announced on the official Django site, the Django 5.0 Version is out. The changes provided in this version are listed below.

Curious minds and cutting-edge developers can test the new features by using the latest Django 5.x release:

$ pip install Django==5.0a1

In order to make this article more useful, here is an open-source Django Starter already updated to use django==5.0a1 version and deployed on Render via a CI/CD flow:

Django v5.x Datta Able - Free Starter updated to Django 5.x Version, crafted by AppSeed.


โœ… Python Compatibility

Django 5.x supports Python 3.10, 3.11, and 3.12. Please note the 4.2.x versions are the last to support Python 3.8 and 3.9.


โœ… Facet filters in the admin

Facet counts are now shown for applied filters in the admin change list when toggled on via the UI. This behavior can be changed via the new ModelAdmin.show_facets attribute.


โœ… Simplified templates for form field rendering

Django 5.x introduces two new concepts that simplify the rendering of the related elements of a Django form field such as its label, widget, help text, and errors.

  • "field group"

  • "field group templates"

Here is the official sample:

<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-computed default values

The new field added, Field.db_default, parameter sets a database-computed default value.

Here is the official sample:

from django.db import models
from django.db.models.functions import Now, Pi


class MyModel(models.Model):
    age = models.IntegerField(db_default=18)
    created = models.DateTimeField(db_default=Now())
    circumference = models.FloatField(db_default=2 * Pi())

โœ… Database generated model field

The new GeneratedField allows the creation of database-generated columns. The official sample:

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"), db_persist=True)

โœ… Options for declaring field choices

Field.choices(for model fields) and ChoiceField.choices(for form fields) allow for more flexibility when declaring their values. In previous versions of Django, choices should either be a list of 2-tuples, or an Enumeration types subclass, but the latter required accessing the .choices attribute to provide the values in the expected form

Django 5.0 adds support for accepting a mapping or a callable instead of an iterable, and also no longer requires .choices to be used directly to expand enumeration types.

Here is the official sample:

from django.db import models

Medal = models.TextChoices("Medal", "GOLD SILVER BRONZE")

SPORT_CHOICES = {  # Using a mapping instead of a list of 2-tuples.
    "Martial Arts": {"judo": "Judo", "karate": "Karate"},
    "Racket": {"badminton": "Badminton", "tennis": "Tennis"},
    "unknown": "Unknown",
}

def get_scores():
    return [(i, str(i)) for i in range(10)]

class Winner(models.Model):
    medal = models.CharField(..., choices=Medal)  # Using `.choices` not required.
    sport = models.CharField(..., choices=SPORT_CHOICES)
    score = models.IntegerField(choices=get_scores)  # A callable is allowed.

โœ… Other minor changes

  • django.contrib.admin - jQuery is upgraded to 3.7.1.

  • django.contrib.auth - The new asynchronous functions are now provided, using an a prefix: django.contrib.auth.aauthenticate(), aget_user(), alogin(), alogout(), and aupdate_session_auth_hash().

  • Asynchronous views - Under ASGI, http.disconnect events are now handled. This allows views to perform any necessary cleanup if a client disconnects before the response is generated.


โœ… In Summary

Django is a high-level Python web framework that simplifies and accelerates web development by providing a robust set of tools, libraries, and conventions. It emphasizes rapid development, clean and pragmatic design, and follows the "Don't Repeat Yourself" (DRY) principle.

Since its first release in 2003, Django has been continuously improved by a super-active community where all the new features are discussed, voted, implemented, and later released.

With built-in features for handling databases, authentication, templating, and more, Django empowers developers to build scalable and secure web applications efficiently.

Django's history is characterized by its commitment to simplicity, best practices, and robustness. It has evolved to adapt to the changing landscape of web development while maintaining a strong focus on developer productivity and code quality.

Today, Django continues to be a widely used and respected web framework, with an active community of developers and a rich ecosystem of packages and extensions.


โœ… Resourcesโ€‹

ย