1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Refs #12990 -- Moved CheckFieldDefaultMixin to the django.db.models.fields.mixins.

This commit is contained in:
sage
2019-10-17 11:36:39 +02:00
committed by Mariusz Felisiak
parent 187a64608d
commit 6f82df69ef
9 changed files with 40 additions and 38 deletions

View File

@@ -1,3 +1,5 @@
from django.core import checks
NOT_PROVIDED = object()
@@ -24,3 +26,31 @@ class FieldCacheMixin:
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