Smarter ModelBackend: select_related your user profile

I have a user profile model for every project I build in Django, so when django-debug-toolbar told me that the auth app was sloppily selecting just my User object, then making another trip back to the database when I called get_profile(), I knew this would not stand. Here’s a quick little patch to select your user and profile data in one query.

Somewhere in your project (utils or misc file would be good), add this snippet:

Then in your settings:

After adding this code, make sure to logout of your app and back in (Django caches your backend after logging in, so it will keep going back to the original ModelBackend if you don’t). You should see one less query in your debug tools.

If you frequently access a model even deeper than your profile, you can easily change up the 'profile' to something like 'profile__company' and fetch them all in a single query.