Django 4.0 Is Out — Here Is Everything About It
TL;DR: Major features in 4.0 are data bases related e.g. example functional unique constraints for database functions, native redis support
So, the Django Team has finally released Django 4.0. Lets deep deeper into some of the features:
1. Functional unique constraints¶
The new *expressions
positional argument of UniqueConstraint()
allows to create functional unique constraints on expressions and database functions. For example:
from django.db import models
from django.db.models import UniqueConstraint
from django.db.models.functions import Lower
class MyModel(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255) class Meta:
constraints = [
UniqueConstraint(
Lower('first_name'),
Lower('last_name').desc(),
name='first_last_name_unique',
),
]
Functional unique constraints are added to models using the Meta.constraints
option.
2. Redis cache backend¶
The new django.core.cache.backends.redis.RedisCache
also allows a built-in support for caching with Redis. redis-py 3.0.0 or higher is required. In the past I used https://github.com/jazzband/django-redis lib.
3. Random testing order
Now it is possible to execute tests in a random order. You can achieve that by
from django.test.runner import DiscoverRunner
class TestRunner(DiscoverRunner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.shuffle is False:
self.shuffle = None # will generate a radom seed
4. Scrypt password hasher
A scrypt password hasher is more secure and recommended over the PBKDF2 algorithm. But it is not the default, as it requires OpenSSL 1.1 and additional memory.
A list of all new features can be found under https://docs.djangoproject.com/en/4.0/releases/4.0/