[Answer]-Context processors, caching

1👍

It works and it’s not unnecessary practice, having a good cache is key however I’m a bit confused as to why you’re looping over the Work QuerySet as you do.

Wouldn’t this work for you:

#custom context processor
works = cache.get(cache_key_works)
if not works:
    drawings = Work.objects.filter(publish=1, category__root__slug='drawing').order_by('-publish_from')
    web_designs = Work.objects.filter(publish=1, category__root__slug='web_designs').order_by('-publish_from')
    worktypes = {'drawings': drawings, 'web_designs': web_designs}

return {'work_types': work_types}

NOTE: The category__root__slug='drawing' is just conceptually since you need to replace it with your correct model structure. Also you’re never putting the work_types back into the cache if you get a cache miss, which is something you should look over too.

Leave a comment