July 23, 2025
Thoughts on Django ORM and SimpleLazyObject
Just recently I wanted to create a custom middleware to augment the request
object by adding an additional attribute to it. But I wanted this attribute to be lazy evaluated. If you have experience building with Django you should know that it offers lazy functions like reverse_lazy
. Looking at the internals of how such things work, I've found that Django offers a module django.utils.functional.py
that contains interesting functions and classes.
I liked SimpleLazyObject
and found that it is used in the middleware calleddjango.contrib.auth.middleware.AuthenticationMiddleware
.