
Note that is sometimes used for slugs, but is not converted because it might be incompatible. Thus, you should test affected paths after this fixer makes any changes. This is not guaranteed backwards compatible: there is a chance that the view expects a string, rather than the converted type.īut, pragmatically, it seems 99.9% of views do not require strings, and instead work with either strings or the converted type. These are taken from the path converter classes.įor some cases, this change alters the type of the arguments passed to the view, from str to the converted type (e.g.


…or PowerShell’s ForEach-Object: git ls-files - '*.py' | % → uuid Some fixers depend on the names of containing directories to activate, so ensure you run djagno-upgrade with paths relative to the root of your project.įor example, with git ls-files | xargs: git ls-files - '*.py' | xargs django-upgrade -target-version 4.1 Use the pre-commit integration, globbing, or another technique for applying to many files. Run django-upgrade before formatters like Black.ĭjango-upgrade does not have any ability to recurse through directories. The -target-version flag defaults to 2.2, the oldest supported version when this project was created.įor more on usage run django-upgrade -help.ĭjango-upgrade focuses on upgrading your code and not on making it look nice. These fixers rewrite your code to avoid DeprecationWarnings and use some new features.įor example: django-upgrade -target-version 4.1 example/core/models.py example/settings.py to the -target-version flag.ĭjango-upgrade will run all its fixers for versions up to and including the target version. I wrote django-upgrade whilst working on the book!ĭjango-upgrade is a commandline tool that rewrites files in place. Pre-commit’s autoupdate command will also let you take advantage of future django-upgrade features.Ĭheck out my book Boost Your Django DX which covers using pre-commit, django-upgrade, and many other tools. Keep the hook installed in order to upgrade all code added to your project.


In the process, your other hooks will run, potentially reformatting django-upgrade’s changes to match your project’s code style. Then, upgrade your entire project: pre-commit run django-upgrade -all-files pre-commit-config.yaml file ( docs), above any code formatters (such as Black): - repo : rev : "" # replace with latest tag on GitHub hooks : - id : django-upgrade args : # Replace with Django version You can also install django-upgrade as a pre-commit hook.Īdd the following to the repos section of your. Use pip: python -m pip install django-upgrade Automatically upgrade your Django project code.
