Refs #12990 -- Moved CheckFieldDefaultMixin to the django.db.models.fields.mixins.
This commit is contained in:
parent
187a64608d
commit
6f82df69ef
|
@ -5,11 +5,11 @@ from django.contrib.postgres.forms import SimpleArrayField
|
|||
from django.contrib.postgres.validators import ArrayMaxLengthValidator
|
||||
from django.core import checks, exceptions
|
||||
from django.db.models import Field, IntegerField, Transform
|
||||
from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
||||
from django.db.models.lookups import Exact, In
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ..utils import prefix_validation_error
|
||||
from .mixins import CheckFieldDefaultMixin
|
||||
from .utils import AttributeSetter
|
||||
|
||||
__all__ = ['ArrayField']
|
||||
|
|
|
@ -4,10 +4,9 @@ from django.contrib.postgres import forms, lookups
|
|||
from django.contrib.postgres.fields.array import ArrayField
|
||||
from django.core import exceptions
|
||||
from django.db.models import Field, TextField, Transform
|
||||
from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .mixins import CheckFieldDefaultMixin
|
||||
|
||||
__all__ = ['HStoreField']
|
||||
|
||||
|
||||
|
|
|
@ -7,10 +7,9 @@ from django.core import exceptions
|
|||
from django.db.models import (
|
||||
Field, TextField, Transform, lookups as builtin_lookups,
|
||||
)
|
||||
from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .mixins import CheckFieldDefaultMixin
|
||||
|
||||
__all__ = ['JSONField']
|
||||
|
||||
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
from django.core import checks
|
||||
|
||||
|
||||
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='postgres.E003',
|
||||
)
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
def check(self, **kwargs):
|
||||
errors = super().check(**kwargs)
|
||||
errors.extend(self._check_default())
|
||||
return errors
|
|
@ -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
|
||||
|
|
|
@ -140,6 +140,8 @@ Model fields
|
|||
* **fields.E008**: All ``validators`` must be callable.
|
||||
* **fields.E009**: ``max_length`` is too small to fit the longest value in
|
||||
``choices`` (``<count>`` characters).
|
||||
* **fields.E010**: ``<field>`` default should be a callable instead of an
|
||||
instance so that it's not shared between all field instances.
|
||||
* **fields.E100**: ``AutoField``\s must set primary_key=True.
|
||||
* **fields.E110**: ``BooleanField``\s do not accept null values. *This check
|
||||
appeared before support for null values was added in Django 2.1.*
|
||||
|
@ -747,7 +749,8 @@ fields:
|
|||
* **postgres.E001**: Base field for array has errors: ...
|
||||
* **postgres.E002**: Base field for array cannot be a related field.
|
||||
* **postgres.E003**: ``<field>`` default should be a callable instead of an
|
||||
instance so that it's not shared between all field instances.
|
||||
instance so that it's not shared between all field instances. *This check was
|
||||
changed to* ``fields.E010`` *in Django 3.1*.
|
||||
|
||||
``sites``
|
||||
---------
|
||||
|
|
|
@ -551,7 +551,7 @@ class TestChecks(PostgreSQLSimpleTestCase):
|
|||
),
|
||||
hint='Use a callable instead, e.g., use `list` instead of `[]`.',
|
||||
obj=MyModel._meta.get_field('field'),
|
||||
id='postgres.E003',
|
||||
id='fields.E010',
|
||||
)
|
||||
])
|
||||
|
||||
|
|
|
@ -231,7 +231,7 @@ class TestChecks(PostgreSQLSimpleTestCase):
|
|||
),
|
||||
hint='Use a callable instead, e.g., use `dict` instead of `{}`.',
|
||||
obj=MyModel._meta.get_field('field'),
|
||||
id='postgres.E003',
|
||||
id='fields.E010',
|
||||
)
|
||||
])
|
||||
|
||||
|
|
|
@ -429,7 +429,7 @@ class TestChecks(PostgreSQLSimpleTestCase):
|
|||
),
|
||||
hint='Use a callable instead, e.g., use `dict` instead of `{}`.',
|
||||
obj=MyModel._meta.get_field('field'),
|
||||
id='postgres.E003',
|
||||
id='fields.E010',
|
||||
)
|
||||
])
|
||||
|
||||
|
|
Loading…
Reference in New Issue