Automatic prefetching of ForeignKeyFields

I have created a library for automatically doing prefetch_related on ForeignKeyFields and OneToOneFields.
Once setup this is totally transparent and will never increase the number of database queries your app is doing, but in a lot of situations it will substantially reduce the number.

It’s based on ideas @adamchainz and I developed when we worked together several years ago. I’ve run it in two non trivial production systems with no issues and friends have tested it in several others.

There’s a longer description in the readme https://pypi.org/project/django-auto-prefetch/ of what it does and why I believe this behaviour is both safe and desirable.

2 Likes

I’ve also tested this with three client projects in the past couple weeks since Gordon created the package. Tests all pass with no errors, with two of them using it in production (so far).