diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2013-06-19 12:03:20 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2013-06-19 12:05:23 +0100 |
| commit | 9daf81b94e7945d7adfdf62206cf9cb436166f0d (patch) | |
| tree | 0ca2a77977432d0c139886297afb433569b588ca /django/db/models/fields/__init__.py | |
| parent | 315ab41e416c777d4f42932d42df07872e8f8895 (diff) | |
| parent | d9a43545be1af95a13c181c8b178f5631d3a4148 (diff) | |
Merge remote-tracking branch 'core/master' into schema-alteration
Conflicts:
django/db/models/loading.py
Diffstat (limited to 'django/db/models/fields/__init__.py')
| -rw-r--r-- | django/db/models/fields/__init__.py | 142 |
1 files changed, 91 insertions, 51 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 691eeffb08..7f3242b213 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -77,7 +77,7 @@ class Field(object): auto_creation_counter = -1 default_validators = [] # Default set of validators default_error_messages = { - 'invalid_choice': _('Value %r is not a valid choice.'), + 'invalid_choice': _('Value %(value)r is not a valid choice.'), 'null': _('This field cannot be null.'), 'blank': _('This field cannot be blank.'), 'unique': _('%(model_name)s with this %(field_label)s ' @@ -294,12 +294,9 @@ class Field(object): v(value) except exceptions.ValidationError as e: if hasattr(e, 'code') and e.code in self.error_messages: - message = self.error_messages[e.code] - if e.params: - message = message % e.params - errors.append(message) - else: - errors.extend(e.messages) + e.message = self.error_messages[e.code] + errors.extend(e.error_list) + if errors: raise exceptions.ValidationError(errors) @@ -322,14 +319,17 @@ class Field(object): return elif value == option_key: return - msg = self.error_messages['invalid_choice'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid_choice'], + code='invalid_choice', + params={'value': value}, + ) if value is None and not self.null: - raise exceptions.ValidationError(self.error_messages['null']) + raise exceptions.ValidationError(self.error_messages['null'], code='null') if not self.blank and value in self.empty_values: - raise exceptions.ValidationError(self.error_messages['blank']) + raise exceptions.ValidationError(self.error_messages['blank'], code='blank') def clean(self, value, model_instance): """ @@ -678,7 +678,7 @@ class AutoField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be an integer."), + 'invalid': _("'%(value)s' value must be an integer."), } def __init__(self, *args, **kwargs): @@ -702,8 +702,11 @@ class AutoField(Field): try: return int(value) except (TypeError, ValueError): - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def validate(self, value, model_instance): pass @@ -732,7 +735,7 @@ class AutoField(Field): class BooleanField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be either True or False."), + 'invalid': _("'%(value)s' value must be either True or False."), } description = _("Boolean (Either True or False)") @@ -757,8 +760,11 @@ class BooleanField(Field): return True if value in ('f', 'False', '0'): return False - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def get_prep_lookup(self, lookup_type, value): # Special-case handling for filters coming from a Web request (e.g. the @@ -830,9 +836,9 @@ class CommaSeparatedIntegerField(CharField): class DateField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value has an invalid date format. It must be " + 'invalid': _("'%(value)s' value has an invalid date format. It must be " "in YYYY-MM-DD format."), - 'invalid_date': _("'%s' value has the correct format (YYYY-MM-DD) " + 'invalid_date': _("'%(value)s' value has the correct format (YYYY-MM-DD) " "but it is an invalid date."), } description = _("Date (without time)") @@ -878,11 +884,17 @@ class DateField(Field): if parsed is not None: return parsed except ValueError: - msg = self.error_messages['invalid_date'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid_date'], + code='invalid_date', + params={'value': value}, + ) - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def pre_save(self, model_instance, add): if self.auto_now or (self.auto_now_add and add): @@ -930,11 +942,11 @@ class DateField(Field): class DateTimeField(DateField): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value has an invalid format. It must be in " + 'invalid': _("'%(value)s' value has an invalid format. It must be in " "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."), - 'invalid_date': _("'%s' value has the correct format " + 'invalid_date': _("'%(value)s' value has the correct format " "(YYYY-MM-DD) but it is an invalid date."), - 'invalid_datetime': _("'%s' value has the correct format " + 'invalid_datetime': _("'%(value)s' value has the correct format " "(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) " "but it is an invalid date/time."), } @@ -969,19 +981,28 @@ class DateTimeField(DateField): if parsed is not None: return parsed except ValueError: - msg = self.error_messages['invalid_datetime'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid_datetime'], + code='invalid_datetime', + params={'value': value}, + ) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: - msg = self.error_messages['invalid_date'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid_date'], + code='invalid_date', + params={'value': value}, + ) - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def pre_save(self, model_instance, add): if self.auto_now or (self.auto_now_add and add): @@ -1027,7 +1048,7 @@ class DateTimeField(DateField): class DecimalField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be a decimal number."), + 'invalid': _("'%(value)s' value must be a decimal number."), } description = _("Decimal number") @@ -1053,8 +1074,11 @@ class DecimalField(Field): try: return decimal.Decimal(value) except decimal.InvalidOperation: - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def _format(self, value): if isinstance(value, six.string_types) or value is None: @@ -1162,7 +1186,7 @@ class FilePathField(Field): class FloatField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be a float."), + 'invalid': _("'%(value)s' value must be a float."), } description = _("Floating point number") @@ -1180,8 +1204,11 @@ class FloatField(Field): try: return float(value) except (TypeError, ValueError): - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def formfield(self, **kwargs): defaults = {'form_class': forms.FloatField} @@ -1191,7 +1218,7 @@ class FloatField(Field): class IntegerField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be an integer."), + 'invalid': _("'%(value)s' value must be an integer."), } description = _("Integer") @@ -1215,8 +1242,11 @@ class IntegerField(Field): try: return int(value) except (TypeError, ValueError): - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def formfield(self, **kwargs): defaults = {'form_class': forms.IntegerField} @@ -1314,7 +1344,7 @@ class GenericIPAddressField(Field): class NullBooleanField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value must be either None, True or False."), + 'invalid': _("'%(value)s' value must be either None, True or False."), } description = _("Boolean (Either True, False or None)") @@ -1343,8 +1373,11 @@ class NullBooleanField(Field): return True if value in ('f', 'False', '0'): return False - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def get_prep_lookup(self, lookup_type, value): # Special-case handling for filters coming from a Web request (e.g. the @@ -1393,6 +1426,7 @@ class PositiveSmallIntegerField(IntegerField): return super(PositiveSmallIntegerField, self).formfield(**defaults) class SlugField(CharField): + default_validators = [validators.validate_slug] description = _("Slug (up to %(max_length)s)") def __init__(self, *args, **kwargs): @@ -1445,9 +1479,9 @@ class TextField(Field): class TimeField(Field): empty_strings_allowed = False default_error_messages = { - 'invalid': _("'%s' value has an invalid format. It must be in " + 'invalid': _("'%(value)s' value has an invalid format. It must be in " "HH:MM[:ss[.uuuuuu]] format."), - 'invalid_time': _("'%s' value has the correct format " + 'invalid_time': _("'%(value)s' value has the correct format " "(HH:MM[:ss[.uuuuuu]]) but it is an invalid time."), } description = _("Time") @@ -1487,11 +1521,17 @@ class TimeField(Field): if parsed is not None: return parsed except ValueError: - msg = self.error_messages['invalid_time'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid_time'], + code='invalid_time', + params={'value': value}, + ) - msg = self.error_messages['invalid'] % value - raise exceptions.ValidationError(msg) + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) def pre_save(self, model_instance, add): if self.auto_now or (self.auto_now_add and add): @@ -1520,12 +1560,12 @@ class TimeField(Field): return super(TimeField, self).formfield(**defaults) class URLField(CharField): + default_validators = [validators.URLValidator()] description = _("URL") def __init__(self, verbose_name=None, name=None, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) CharField.__init__(self, verbose_name, name, **kwargs) - self.validators.append(validators.URLValidator()) def deconstruct(self): name, path, args, kwargs = super(URLField, self).deconstruct() |
