mirror of https://github.com/django/django.git
Fixed #8206 -- Removed validate methods of Model and Model fields. They are are unsupported for 1.0 and will be replaced with more complete model validation (refs #6845).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8348 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
29a9c34c65
commit
788de6b5fd
|
@ -65,3 +65,7 @@ class Session(models.Model):
|
||||||
# just return an empty dictionary (an empty session).
|
# just return an empty dictionary (an empty session).
|
||||||
except:
|
except:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
class SessionAdmin(admin.ModelAdmin):
|
||||||
|
pass
|
||||||
|
|
|
@ -368,28 +368,6 @@ class Model(object):
|
||||||
|
|
||||||
save_base.alters_data = True
|
save_base.alters_data = True
|
||||||
|
|
||||||
def validate(self):
|
|
||||||
"""
|
|
||||||
First coerces all fields on this instance to their proper Python types.
|
|
||||||
Then runs validation on every field. Returns a dictionary of
|
|
||||||
field_name -> error_list.
|
|
||||||
"""
|
|
||||||
error_dict = {}
|
|
||||||
invalid_python = {}
|
|
||||||
for f in self._meta.fields:
|
|
||||||
try:
|
|
||||||
setattr(self, f.attname, f.to_python(getattr(self, f.attname, f.get_default())))
|
|
||||||
except validators.ValidationError, e:
|
|
||||||
error_dict[f.name] = e.messages
|
|
||||||
invalid_python[f.name] = 1
|
|
||||||
for f in self._meta.fields:
|
|
||||||
if f.name in invalid_python:
|
|
||||||
continue
|
|
||||||
errors = f.validate_full(getattr(self, f.attname, f.get_default()), self.__dict__)
|
|
||||||
if errors:
|
|
||||||
error_dict[f.name] = errors
|
|
||||||
return error_dict
|
|
||||||
|
|
||||||
def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
|
def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
|
||||||
"""
|
"""
|
||||||
Recursively populates seen_objs with all objects related to this
|
Recursively populates seen_objs with all objects related to this
|
||||||
|
|
|
@ -160,29 +160,6 @@ class Field(object):
|
||||||
return self._unique or self.primary_key
|
return self._unique or self.primary_key
|
||||||
unique = property(unique)
|
unique = property(unique)
|
||||||
|
|
||||||
def validate_full(self, field_data, all_data):
|
|
||||||
"""
|
|
||||||
Returns a list of errors for this field. This is the main interface,
|
|
||||||
as it encapsulates some basic validation logic used by all fields.
|
|
||||||
Subclasses should implement validate(), not validate_full().
|
|
||||||
"""
|
|
||||||
if not self.blank and not field_data:
|
|
||||||
return [_('This field is required.')]
|
|
||||||
try:
|
|
||||||
self.validate(field_data, all_data)
|
|
||||||
except validators.ValidationError, e:
|
|
||||||
return e.messages
|
|
||||||
return []
|
|
||||||
|
|
||||||
def validate(self, field_data, all_data):
|
|
||||||
"""
|
|
||||||
Raises validators.ValidationError if field_data has any errors.
|
|
||||||
Subclasses should override this to specify field-specific validation
|
|
||||||
logic. This method should assume field_data has already been converted
|
|
||||||
into the appropriate data type by Field.to_python().
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def set_attributes_from_name(self, name):
|
def set_attributes_from_name(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.attname, self.column = self.get_attname_column()
|
self.attname, self.column = self.get_attname_column()
|
||||||
|
@ -750,9 +727,6 @@ class EmailField(CharField):
|
||||||
def get_manipulator_field_objs(self):
|
def get_manipulator_field_objs(self):
|
||||||
return [oldforms.EmailField]
|
return [oldforms.EmailField]
|
||||||
|
|
||||||
def validate(self, field_data, all_data):
|
|
||||||
validators.isValidEmail(field_data, all_data)
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': forms.EmailField}
|
defaults = {'form_class': forms.EmailField}
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
|
@ -829,9 +803,6 @@ class IPAddressField(Field):
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
return "IPAddressField"
|
return "IPAddressField"
|
||||||
|
|
||||||
def validate(self, field_data, all_data):
|
|
||||||
validators.isValidIPAddress4(field_data, None)
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': forms.IPAddressField}
|
defaults = {'form_class': forms.IPAddressField}
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
|
@ -877,9 +848,6 @@ class PhoneNumberField(Field):
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
return "PhoneNumberField"
|
return "PhoneNumberField"
|
||||||
|
|
||||||
def validate(self, field_data, all_data):
|
|
||||||
validators.isValidPhone(field_data, all_data)
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
from django.contrib.localflavor.us.forms import USPhoneNumberField
|
from django.contrib.localflavor.us.forms import USPhoneNumberField
|
||||||
defaults = {'form_class': USPhoneNumberField}
|
defaults = {'form_class': USPhoneNumberField}
|
||||||
|
|
|
@ -1,177 +0,0 @@
|
||||||
"""
|
|
||||||
31. Validation
|
|
||||||
|
|
||||||
This is an experimental feature!
|
|
||||||
|
|
||||||
Each model instance has a validate() method that returns a dictionary of
|
|
||||||
validation errors in the instance's fields. This method has a side effect
|
|
||||||
of converting each field to its appropriate Python data type.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
class Person(models.Model):
|
|
||||||
is_child = models.BooleanField()
|
|
||||||
name = models.CharField(max_length=20)
|
|
||||||
birthdate = models.DateField()
|
|
||||||
favorite_moment = models.DateTimeField()
|
|
||||||
email = models.EmailField()
|
|
||||||
best_time = models.TimeField()
|
|
||||||
|
|
||||||
def __unicode__(self):
|
|
||||||
return self.name
|
|
||||||
|
|
||||||
__test__ = {'API_TESTS':"""
|
|
||||||
|
|
||||||
>>> import datetime
|
|
||||||
>>> valid_params = {
|
|
||||||
... 'is_child': True,
|
|
||||||
... 'name': 'John',
|
|
||||||
... 'birthdate': datetime.date(2000, 5, 3),
|
|
||||||
... 'favorite_moment': datetime.datetime(2002, 4, 3, 13, 23),
|
|
||||||
... 'email': 'john@example.com',
|
|
||||||
... 'best_time': datetime.time(16, 20),
|
|
||||||
... }
|
|
||||||
>>> p = Person(**valid_params)
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, id='23'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.id
|
|
||||||
23
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, id='foo'))
|
|
||||||
>>> p.validate()['id']
|
|
||||||
[u'This value must be an integer.']
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, id=None))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> repr(p.id)
|
|
||||||
'None'
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, is_child='t'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.is_child
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, is_child='f'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.is_child
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, is_child=True))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.is_child
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, is_child=False))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.is_child
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, is_child='foo'))
|
|
||||||
>>> p.validate()['is_child']
|
|
||||||
[u'This value must be either True or False.']
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, name=u'Jose'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.name
|
|
||||||
u'Jose'
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, name=227))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.name
|
|
||||||
u'227'
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.birthdate
|
|
||||||
datetime.date(2000, 5, 3)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, birthdate=datetime.datetime(2000, 5, 3)))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.birthdate
|
|
||||||
datetime.date(2000, 5, 3)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, birthdate='2000-05-03'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.birthdate
|
|
||||||
datetime.date(2000, 5, 3)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, birthdate='2000-5-3'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.birthdate
|
|
||||||
datetime.date(2000, 5, 3)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, birthdate='foo'))
|
|
||||||
>>> p.validate()['birthdate']
|
|
||||||
[u'Enter a valid date in YYYY-MM-DD format.']
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23)))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.favorite_moment
|
|
||||||
datetime.datetime(2002, 4, 3, 13, 23)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3)))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.favorite_moment
|
|
||||||
datetime.datetime(2002, 4, 3, 0, 0)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, best_time='16:20:00'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.best_time
|
|
||||||
datetime.time(16, 20)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, best_time='16:20'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.best_time
|
|
||||||
datetime.time(16, 20)
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, best_time='bar'))
|
|
||||||
>>> p.validate()['best_time']
|
|
||||||
[u'Enter a valid time in HH:MM[:ss[.uuuuuu]] format.']
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, email='john@example.com'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.email
|
|
||||||
'john@example.com'
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, email=u'john@example.com'))
|
|
||||||
>>> p.validate()
|
|
||||||
{}
|
|
||||||
>>> p.email
|
|
||||||
u'john@example.com'
|
|
||||||
|
|
||||||
>>> p = Person(**dict(valid_params, email=22))
|
|
||||||
>>> p.validate()['email']
|
|
||||||
[u'Enter a valid e-mail address.']
|
|
||||||
|
|
||||||
# Make sure that Date and DateTime return validation errors and don't raise Python errors.
|
|
||||||
>>> p = Person(name='John Doe', is_child=True, email='abc@def.com')
|
|
||||||
>>> errors = p.validate()
|
|
||||||
>>> errors['favorite_moment']
|
|
||||||
[u'This field is required.']
|
|
||||||
>>> errors['birthdate']
|
|
||||||
[u'This field is required.']
|
|
||||||
>>> errors['best_time']
|
|
||||||
[u'This field is required.']
|
|
||||||
|
|
||||||
"""}
|
|
Loading…
Reference in New Issue