What is new in Django 4.0

Posted by Afsal on 23-Dec-2021

Django 4.0 was released recently. It is compatible with Python 3.8 and above. Django 4.0 comes with some major and many minor changes, Let us discuss the major changes in Django 4.0

1. Zoneinfo default timezone implementation

Now, Django uses the tzinfo from the datetime module in the Python standard library. Previously Django used pytz library. Django 4.0 allows the use of pytz as default by using the settings USE_DEPRECATED_PYTZ. This feature will be removed in Django 5.0

2. Functional unique constraints

Django gives more convenience to the users  by giving functional unique constraints. This helps us to create complicated unique constraints.

For example

from django.db import models
from django.db.models import UniqueConstraint

class MobileNumber(models.Model):
    country_code = models.CharField(max_length=10)
    mobile_number = models.CharField(max_length=16)

    class Meta:
        constraints = [
            UniqueConstraint(
                'country_code',
                'mobile_number',
                name='country_code_mobile_number_unique',
            ),
        ]

Here, the unique constraint can enforce based on two fields

3. Scrypt password hasher

In Django 4.0, there is a provision to use scrypt password hashing. It is recommended over the PBKDF2 password hasher. Scrypt password is not default as it requires OpenSSLl 1.1+ and more memory.

4. Redis cache backend

Django 4.0 provides native support for redis. The new django.core.cache.backends.redis.RedisCache cache backend provides built-in support for caching with Redis. 

5. Template based form rendering

In Django 4.0, Form rendering is done based on a template that can be customized and changed according to our needs.

Other than the additions discussed, there are so many minor changes, deprecation and removals. You can refer to the Django official documentation by clicking this link.