diff options
| author | Simon Charette <charette.s@gmail.com> | 2014-03-25 21:40:56 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2014-03-26 12:57:57 -0400 |
| commit | b9e50e47742f67dcf3c37aad989b350e8255154e (patch) | |
| tree | 768648f9afd29ce741acf2f4b6c9aa47ec6c2db0 | |
| parent | 5233b366935dcc81e47546921ab4eb5007c83719 (diff) | |
Fixed the PostGIS circular imports caused by 1506c71a95.
Thanks to @loic for the help and @timgraham for the review.
refs #12030.
| -rw-r--r-- | django/db/models/fields/__init__.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 268a0bcab8..964f71e694 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -20,7 +20,7 @@ from django.core import exceptions, validators, checks from django.utils.datastructures import DictWrapper from django.utils.dateparse import parse_date, parse_datetime, parse_time from django.utils.deprecation import RemovedInDjango19Warning -from django.utils.functional import curry, total_ordering, Promise +from django.utils.functional import cached_property, curry, total_ordering, Promise from django.utils.text import capfirst from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -157,7 +157,6 @@ class Field(RegisterLookupMixin): Field.creation_counter += 1 self._validators = validators # Store for deconstruction later - self.validators = self.default_validators + validators messages = {} for c in reversed(self.__class__.__mro__): @@ -447,6 +446,12 @@ class Field(RegisterLookupMixin): """ return value + @cached_property + def validators(self): + # Some validators can't be created at field initialization time. + # This method provides a way to delay their creation until required. + return self.default_validators + self._validators + def run_validators(self, value): if value in self.empty_values: return @@ -1561,16 +1566,18 @@ class IntegerField(Field): } description = _("Integer") - def __init__(self, *args, **kwargs): - default_validators = self.default_validators[:] + @cached_property + def validators(self): + # These validators can't be added at field initialization time since + # they're based on values retrieved from `connection`. + range_validators = [] internal_type = self.get_internal_type() min_value, max_value = connection.ops.integer_field_range(internal_type) if min_value is not None: - default_validators.append(validators.MinValueValidator(min_value)) + range_validators.append(validators.MinValueValidator(min_value)) if max_value is not None: - default_validators.append(validators.MaxValueValidator(max_value)) - self.default_validators = default_validators - super(IntegerField, self).__init__(*args, **kwargs) + range_validators.append(validators.MaxValueValidator(max_value)) + return super(IntegerField, self).validators + range_validators def get_prep_value(self, value): value = super(IntegerField, self).get_prep_value(value) |
