provide an api for working with the model s fields value cache def getcachenameself raise notimplementederror def getcachedvalueself instance defaultnotprovided cachename self getcachename try return instance state fieldscachecachename except keyerror if default is notprovided raise return default def iscachedself instance return self getcachename in instance state fieldscache def setcachedvalueself instance value instance state fieldscacheself getcachename value def deletecachedvalueself instance del instance state fieldscacheself getcachename class checkfielddefaultmixin defaulthint valid default invalid default def checkdefaultself if self hasdefault and self default is not none and not callableself default return checks warning s default should be a callable instead of an instance so that it s not shared between all field instances self class name hint use a callable instead e g use s instead of s self defaulthint objself idfields e010 else return def checkself kwargs errors super checkkwargs errors extendself checkdefault return errors provide an api for working with the model s fields value cache
from django.core import checks

NOT_PROVIDED = object()


class FieldCacheMixin:
    

    def get_cache_name(self):
        raise NotImplementedError

    def get_cached_value(self, instance, default=NOT_PROVIDED):
        cache_name = self.get_cache_name()
        try:
            return instance._state.fields_cache[cache_name]
        except KeyError:
            if default is NOT_PROVIDED:
                raise
            return default

    def is_cached(self, instance):
        return self.get_cache_name() in instance._state.fields_cache

    def set_cached_value(self, instance, value):
        instance._state.fields_cache[self.get_cache_name()] = value

    def delete_cached_value(self, instance):
        del instance._state.fields_cache[self.get_cache_name()]


class CheckFieldDefaultMixin:
    _default_hint = ("<valid default>", "<invalid default>")

    def _check_default(self):
        if (
            self.has_default()
            and self.default is not None
            and not callable(self.default)
        ):
            return [
                checks.Warning(
                    "%s default should be a callable instead of an instance "
                    "so that it's not shared between all field instances."
                    % (self.__class__.__name__,),
                    hint=(
                        "Use a callable instead, e.g., use `%s` instead of "
                        "`%s`." % self._default_hint
                    ),
                    obj=self,
                    id="fields.E010",
                )
            ]
        else:
            return []

    def check(self, **kwargs):
        errors = super().check(**kwargs)
        errors.extend(self._check_default())
        return errors