Usage

django-upgrade is a commandline tool that rewrites files to automatically upgrade your code to fix DeprecationWarnings from Django and use some new features. Target files like:

django-upgrade example/core/models.py example/settings.py

django-upgrade focuses on upgrading your code and not on making it look nice. Run django-upgrade before formatters like Black or Ruff.

Some of django-upgrade’s fixers make changes to models that need migrations:

Add a test for pending migrations to ensure that you do not miss these.

django-upgrade does not have any ability to recurse through directories. Use the pre-commit integration, globbing, or another technique for applying to many files. Some fixers depend on the names of containing directories to activate, so ensure you run django-upgrade with paths relative to the root of your project. For example, with git ls-files | xargs:

git ls-files -z -- '*.py' | xargs -0r django-upgrade

…or PowerShell’s ForEach-Object:

git ls-files -- '*.py' | %{django-upgrade $_}

The filename - makes django-upgrade read from standard input and write the updated contents to standard output. In this case, django-upgrade always exits with code 0, even if changes were made.