Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/db/models/fields/mixins.py: 92%
29 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from django.core import checks
3NOT_PROVIDED = object()
6class FieldCacheMixin:
7 """Provide an API for working with the model's fields value cache."""
9 def get_cache_name(self):
10 raise NotImplementedError
12 def get_cached_value(self, instance, default=NOT_PROVIDED):
13 cache_name = self.get_cache_name()
14 try:
15 return instance._state.fields_cache[cache_name]
16 except KeyError:
17 if default is NOT_PROVIDED:
18 raise
19 return default
21 def is_cached(self, instance):
22 return self.get_cache_name() in instance._state.fields_cache
24 def set_cached_value(self, instance, value):
25 instance._state.fields_cache[self.get_cache_name()] = value
27 def delete_cached_value(self, instance):
28 del instance._state.fields_cache[self.get_cache_name()]
31class CheckFieldDefaultMixin:
32 _default_hint = ("<valid default>", "<invalid default>")
34 def _check_default(self):
35 if ( 35 ↛ 40line 35 didn't jump to line 40
36 self.has_default()
37 and self.default is not None
38 and not callable(self.default)
39 ):
40 return [
41 checks.Warning(
42 "%s default should be a callable instead of an instance "
43 "so that it's not shared between all field instances."
44 % (self.__class__.__name__,),
45 hint=(
46 "Use a callable instead, e.g., use `%s` instead of "
47 "`%s`." % self._default_hint
48 ),
49 obj=self,
50 id="fields.E010",
51 )
52 ]
53 else:
54 return []
56 def check(self, **kwargs):
57 errors = super().check(**kwargs)
58 errors.extend(self._check_default())
59 return errors