Django 4.0 Adam Johnson

New Testing Features in Django 4.0

Django 4.0’s five headline features

#1 zoneinfo as the default timezone implementation

Django historically relied on pytz for timezones, but it has… issues.

https://fediverse.org/pganssle contributed zoneinfo to Python’s standard library, to address these. Django now uses zoneinfo.

Thanks to https://fediverse.org/carltongibson for contributing this.

now:

In [6]: import zoneinfo
In [7]: zoneinfo.ZoneInfo("Europe/Paris")
Out[7]: zoneinfo.ZoneInfo(key='Europe/Paris')

Before:

In [8]: import pytz
In [9]: pytz.timezone("Europe/Paris")
Out[9]: <DstTzInfo 'Europe/Paris' LMT+0:09:00 STD>

#2 Functional unique constraints

You can now use database functions to define unique constraints. This allows you to enforce uniqueness on transformed values, e.g. to ensure email addresses unique despite use of upper/lower case

Thanks to https://fediverse.org/hannseman for contributing this.

class Person(models.Model):
...

class Meta:
    constraints = [
        models.UniqueConstraint(
           Lower("email"),
           name ="%(app_label)s_%(class)s_unique_email",
        )
    ]

#3 scrypt password hasher

Django defaults to using PBKDF2 ( https://en.wikipedia.org/wiki/PBKDF2 ) for password hashing.

Scrypt ( https://en.wikipedia.org/wiki/Scrypt ) is a stronger choice that requires more memory, to slow down attacks.

Django now includes a scrypt hasher .

Thanks to :ref`Anthony Wright < https://fediverse.org/Ryo >`) for contributing this

PASSWORD_HASHERS = [
    "django.contrib.auth.hashers.ScrypPasswordHasher",
    ...,
]

#4 Built-in Redis cache backend

https://fediverse.org/Redisinc is very popular with Django developers. Previously it was mainly used via the django-redis package, but now it’s built-in!

Thanks to https://fediverse.org/Abbasidaniyal_ for contributing this as a Google of Summer of Code project.

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
    }
}

Big congratulations to https://fediverse.org/Abbasidaniyal/Abbasidaniyal_ for his successful GSoC project adding a Redis cache backend to Django. Coming to you in Django 4.0. 🎁🎉

../../../_images/carlton.png

https://fediverse.org/carltongibson/status/1437786664760397835?s=20

../../../_images/abbasi.png

https://fediverse.org/Abbasidaniyal_/status/1437802277251661829?s=20

#5 Template-based form rendering

Django already rendered changed to render form widgets through templates.

Now the remaining pieces of the form process go through templates, which you can replace with custom display logic.

Thanks to https://fediverse.org/David_Smith86 and https://fediverse.org/JohannesMaron .