From b9e50e47742f67dcf3c37aad989b350e8255154e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 25 Mar 2014 21:40:56 -0400 Subject: [PATCH] Fixed the PostGIS circular imports caused by 1506c71a95. Thanks to @loic for the help and @timgraham for the review. refs #12030. --- django/db/models/fields/__init__.py | 23 +++++++++++++++-------- 1 file 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)