What is New in Django 4.2
Django 4.2 is a feature release that adds support for Psycopg 3, new options for database comments, and a customizable file storage system. It improves the admin interface with themes and better widgets, enhances GIS and PostgreSQL capabilities, and includes security updates like BREACH mitigation. Several deprecations prepare for future changes, along with some backward incompatible updates.
Psycopg 3 Support
Django now works with the modern psycopg library version 3.1.8 or higher. The database engine setting stays the same, supporting both Psycopg 2 and 3. Note that Psycopg 3 has some differences from version 2.
Database Comments
Add descriptive comments to columns and tables directly in models.
class Question(models.Model):
text = models.TextField(db_comment="Poll question")
pub_date = models.DateTimeField(db_comment="Date and time when the question was published")
class Meta:
db_table_comment = "Poll questions"
Migrations support changing these comments.
Custom File Storages
The new STORAGES setting lets you define multiple backends for default files and static files. It replaces the old DEFAULT_FILE_STORAGE and STATICFILES_STORAGE settings.
Admin Interface Updates
- Light and dark theme toggle, including automatic system preference detection.
- Uses system fonts for faster loading.
- Filter widgets in changelists are now searchable.
- Editable lists use atomic transactions for safety.
- New blocks in templates for easier customization.
GIS Improvements
- GeoJSON serialization includes an
idkey. - New
isemptylookup for empty geometries on PostGIS. - Support for
pathlib.PathinGDALRaster. - Updated OpenLayers widget to version 7.2.2.
Security Enhancements
GZipMiddlewareadds random bytes to responses to protect against BREACH attacks.- Default PBKDF2 password hasher uses more iterations for stronger security.
Other Notable Features
- In-memory storage for faster testing.
- Async support in streaming responses.
- New PostgreSQL lookups like
trigram_strict_word_similar. - Improved error reporting with exception notes on Python 3.11+.
- Central Kurdish language support.
Deprecations
index_togethermodel option; useindexesinstead.- Passing encoded JSON strings to
JSONField. - Some password hashers and case-insensitive fields.
- Old file storage settings.
- Various APIs and template filters.
Backward Incompatible Changes
| Area | Change |
|---|---|
| Database Support | Dropped MariaDB 10.3, MySQL 5.7, and PostgreSQL 11 |
| Model Save | update_or_create() passes update_fields to save() |
| MySQL Aggregations | Raw SQL aggregations no longer allowed in GROUP BY |
| User Forms | UserCreationForm handles many-to-many fields and stricter username checks |
Why Upgrade to Django 4.2
Django 4.2 brings modern database support, better admin usability, and stronger security. It offers tools for cleaner code and faster development while maintaining stability. This release is a solid step forward for projects needing updated features and performance.