django-upgrade
django-upgrade is a tool that automatically upgrades your Django project code. It changes code to avoid deprecation warnings and migrates some patterns to newer Django idioms, easing the upgrade process and helping ensure your code uses the latest features. It is currently at version 1.30.0 and is actively maintained.
Warnings
- gotcha django-upgrade makes changes to your code in-place. It's crucial to run it on a clean Git branch and review changes carefully. Always run your test suite after applying `django-upgrade` to catch any regressions.
- gotcha Some fixers (e.g., for `index_together` or `NullBooleanField`) make changes that necessitate new database migrations. After running `django-upgrade`, you may need to run `python manage.py makemigrations` and then `python manage.py migrate` to apply these schema changes.
- breaking django-upgrade requires Python 3.10 or newer. When upgrading to recent Django versions (e.g., Django 5.2), ensure your Python environment meets the minimum requirements, as older Python versions are no longer supported.
- gotcha django-upgrade focuses on upgrading code to avoid deprecations, not on code style. It's recommended to run `django-upgrade` *before* code formatters like Black or Ruff to avoid conflicts or reformatting changes made by `django-upgrade`.
- gotcha While `django-upgrade` automates many code changes, it does not replace the need to read Django's official release notes, especially for major version upgrades. Manual adjustments for breaking API changes, configuration updates, or third-party package incompatibilities will still be necessary.
- breaking When targeting Django 5.2, ensure your database backend is also compatible. Specifically, PostgreSQL 13 support is dropped (requiring 14+), and MySQL requires version 8.0+.
Install
-
pip install django-upgrade
Quickstart
# To apply fixers for Django 4.2 to all Python files in the current directory: django-upgrade . --target-version 4.2 # Or, to process a specific file and print changes to stdout (without modifying in-place): # django-upgrade myfile.py -