About Caching Function and Method Values

You can cache function and method values with functools.cache decorator:

1
2
3
4
5
6
7
from functools import cache
from django.contrib.auth.models import AbstractUser

def User(AbstractUser):
    @cache
    def is_editor(self):
        return self.groups.filter(name="Editors").exists()

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2 Python 3