What is New in Django 5.2
Django 5.2 introduces composite primary keys, automatic model imports in the shell, and many smaller enhancements across forms, templates, and database tools. It improves customization in rendering and error handling while raising some requirements and preparing deprecations for cleaner future code.
Composite Primary Keys
Models now support primary keys made of multiple fields using the new CompositePrimaryKey class. This allows more flexible database designs without relying on surrogate keys.
from django.db.models import CompositePrimaryKey
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField()
class Meta:
pk = CompositePrimaryKey('order', 'product')
Automatic Model Imports in Shell
The shell command now imports models from all installed apps by default. This saves time when exploring data interactively. Details appear at higher verbosity levels, and you can customize the behavior.
Customizing BoundField Rendering
Override BoundField more easily at project, form, or field levels with new class attributes. This simplifies creating custom form widgets and layouts.
Forms and Widgets
- New input widgets:
ColorInput,SearchInput, andTelInput. ErrorListsupports custom HTML IDs.BoundFieldaddsaria_describedbyfor better accessibility.- Custom attributes for JavaScript in form media via
Scriptasset.
Database and Models
AlterConstraintmigration operation to modify constraints without dropping them.- New
JSONArraydatabase function. - Support for curved geometries in GDAL.
- MySQL gains
coveredbyandcoverslookups. QuerySet.values()andvalues_list()respect expression order.CharField.max_lengthoptional on SQLite.
Admin and Templates
- New
extrabodyblock in adminbase.html. URLFieldvalues render as clickable links.- Custom link text in docstrings.
- Model docs restricted by permissions.
simple_block_tag()decorator for easy block tags.
Other Improvements
- Async support in auth backends.
- Custom error messages in password validators.
- Stronger default PBKDF2 iterations.
- Connection pools for Oracle.
- Async view methods with
method_decorator(). - Improved email attachment handling.
- Better reverse URL with query and fragment args.
Backward Incompatible Changes
- Dropped support for PostgreSQL 13 (now requires 14+).
- MySQL defaults to
utf8mb4charset. - Changes in email attachments and alternatives handling.
- Stricter aggregate argument checks.
- Removed fallbacks in auth when user is None.
Deprecations
- Fallbacks to
request.userin auth functions. orderingin some PostgreSQL aggregates.- Some static files finder arguments.
Why Upgrade to Django 5.2
Django 5.2 adds powerful tools like composite keys and better form customization while keeping projects stable. It enhances database features, security, and developer convenience, making it a great choice for modern web applications.