1👍
Here’s a simple way to memoize your tag:
get_languages_cache = None
@register.simple_tag
def get_languages(obj):
if get_languages_cache is None:
get_languages_cache = obj.get_languages()
return get_languages_cache
This assumes obj.get_languages()
never returns None
. Check this question for further discussion and examples of memoization in Python.
Source:stackexchange.com