[Fixed]-"Lazy load" of data from a context processor

28👍

Django has a SimpleLazyObject. In Django 1.3, this is used by the auth context processor (source code). This makes user available in the template context for every query, but the user is only accessed when the template contains {{ user }}.

You should be able to do something similar in your context processor.

from django.utils.functional import SimpleLazyObject
def my_context_processor(request):
    def complicated_query():
        do_stuff()
        return result

    return {
        'result': SimpleLazyObject(complicated_query)

14👍

If you pass a callable object into the template context, Django will evaluate it when it is used in the template. This provides one simple way to do laziness — just pass in callables:

def my_context_processor(request):
    def complicated_query():
        do_stuff()
        return result                      
    return {'my_info': complicated_query}

The problem with this is it does not memoize the call — if you use it multiple times in a template, complicated_query gets called multiple times.

The fix is to use something like SimpleLazyObject as in the other answer, or to use something like functools.lru_cache:

from functools import lru_cache:

def my_context_processor(request):
    @lru_cache()
    def complicated_query():
        result = do_stuff()
        return result                      
    return {'my_info': complicated_query}

You can now use my_info in your template, and it will be evaluated lazily, just once.

Or, if the function already exists, you would do it like this:

from somewhere import complicated_query

def my_context_processor(request):        
    return {'my_info': lru_cache()(complicated_query)}

I would prefer this method over SimpleLazyObject because the latter can produce some strange bugs sometimes.

(I was the one who originally implemented LazyObject and SimpleLazyObject, and discovered for myself that there is curse on any code artefact labelled simple.)

Leave a comment