Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8206 -- Removed validate methods of Model and Model fields. Th…

…ey 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
  • Loading branch information...
commit 788de6b5fdef05c1f321868db964ffe31949fbac 1 parent 29a9c34
Gary Wilson Jr. authored August 14, 2008
4  django/contrib/sessions/models.py
@@ -65,3 +65,7 @@ def get_decoded(self):
65 65
         # just return an empty dictionary (an empty session).
66 66
         except:
67 67
             return {}
  68
+
  69
+from django.contrib import admin
  70
+class SessionAdmin(admin.ModelAdmin):
  71
+    pass
22  django/db/models/base.py
@@ -368,28 +368,6 @@ def save_base(self, raw=False, cls=None, force_insert=False,
368 368
 
369 369
     save_base.alters_data = True
370 370
 
371  
-    def validate(self):
372  
-        """
373  
-        First coerces all fields on this instance to their proper Python types.
374  
-        Then runs validation on every field. Returns a dictionary of
375  
-        field_name -> error_list.
376  
-        """
377  
-        error_dict = {}
378  
-        invalid_python = {}
379  
-        for f in self._meta.fields:
380  
-            try:
381  
-                setattr(self, f.attname, f.to_python(getattr(self, f.attname, f.get_default())))
382  
-            except validators.ValidationError, e:
383  
-                error_dict[f.name] = e.messages
384  
-                invalid_python[f.name] = 1
385  
-        for f in self._meta.fields:
386  
-            if f.name in invalid_python:
387  
-                continue
388  
-            errors = f.validate_full(getattr(self, f.attname, f.get_default()), self.__dict__)
389  
-            if errors:
390  
-                error_dict[f.name] = errors
391  
-        return error_dict
392  
-
393 371
     def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
394 372
         """
395 373
         Recursively populates seen_objs with all objects related to this
32  django/db/models/fields/__init__.py
@@ -160,29 +160,6 @@ def unique(self):
160 160
         return self._unique or self.primary_key
161 161
     unique = property(unique)
162 162
 
163  
-    def validate_full(self, field_data, all_data):
164  
-        """
165  
-        Returns a list of errors for this field. This is the main interface,
166  
-        as it encapsulates some basic validation logic used by all fields.
167  
-        Subclasses should implement validate(), not validate_full().
168  
-        """
169  
-        if not self.blank and not field_data:
170  
-            return [_('This field is required.')]
171  
-        try:
172  
-            self.validate(field_data, all_data)
173  
-        except validators.ValidationError, e:
174  
-            return e.messages
175  
-        return []
176  
-
177  
-    def validate(self, field_data, all_data):
178  
-        """
179  
-        Raises validators.ValidationError if field_data has any errors.
180  
-        Subclasses should override this to specify field-specific validation
181  
-        logic. This method should assume field_data has already been converted
182  
-        into the appropriate data type by Field.to_python().
183  
-        """
184  
-        pass
185  
-
186 163
     def set_attributes_from_name(self, name):
187 164
         self.name = name
188 165
         self.attname, self.column = self.get_attname_column()
@@ -750,9 +727,6 @@ def __init__(self, *args, **kwargs):
750 727
     def get_manipulator_field_objs(self):
751 728
         return [oldforms.EmailField]
752 729
 
753  
-    def validate(self, field_data, all_data):
754  
-        validators.isValidEmail(field_data, all_data)
755  
-
756 730
     def formfield(self, **kwargs):
757 731
         defaults = {'form_class': forms.EmailField}
758 732
         defaults.update(kwargs)
@@ -829,9 +803,6 @@ def get_manipulator_field_objs(self):
829 803
     def get_internal_type(self):
830 804
         return "IPAddressField"
831 805
 
832  
-    def validate(self, field_data, all_data):
833  
-        validators.isValidIPAddress4(field_data, None)
834  
-
835 806
     def formfield(self, **kwargs):
836 807
         defaults = {'form_class': forms.IPAddressField}
837 808
         defaults.update(kwargs)
@@ -877,9 +848,6 @@ def get_manipulator_field_objs(self):
877 848
     def get_internal_type(self):
878 849
         return "PhoneNumberField"
879 850
 
880  
-    def validate(self, field_data, all_data):
881  
-        validators.isValidPhone(field_data, all_data)
882  
-
883 851
     def formfield(self, **kwargs):
884 852
         from django.contrib.localflavor.us.forms import USPhoneNumberField
885 853
         defaults = {'form_class': USPhoneNumberField}
0  tests/modeltests/validation/__init__.py
No changes.
177  tests/modeltests/validation/models.py
... ...
@@ -1,177 +0,0 @@
1  
-"""
2  
-31. Validation
3  
-
4  
-This is an experimental feature!
5  
-
6  
-Each model instance has a validate() method that returns a dictionary of
7  
-validation errors in the instance's fields. This method has a side effect
8  
-of converting each field to its appropriate Python data type.
9  
-"""
10  
-
11  
-from django.db import models
12  
-
13  
-class Person(models.Model):
14  
-    is_child = models.BooleanField()
15  
-    name = models.CharField(max_length=20)
16  
-    birthdate = models.DateField()
17  
-    favorite_moment = models.DateTimeField()
18  
-    email = models.EmailField()
19  
-    best_time = models.TimeField()
20  
-
21  
-    def __unicode__(self):
22  
-        return self.name
23  
-
24  
-__test__ = {'API_TESTS':"""
25  
-
26  
->>> import datetime
27  
->>> valid_params = {
28  
-...     'is_child': True,
29  
-...     'name': 'John',
30  
-...     'birthdate': datetime.date(2000, 5, 3),
31  
-...     'favorite_moment': datetime.datetime(2002, 4, 3, 13, 23),
32  
-...     'email': 'john@example.com',
33  
-...     'best_time': datetime.time(16, 20),
34  
-... }
35  
->>> p = Person(**valid_params)
36  
->>> p.validate()
37  
-{}
38  
-
39  
->>> p = Person(**dict(valid_params, id='23'))
40  
->>> p.validate()
41  
-{}
42  
->>> p.id
43  
-23
44  
-
45  
->>> p = Person(**dict(valid_params, id='foo'))
46  
->>> p.validate()['id']
47  
-[u'This value must be an integer.']
48  
-
49  
->>> p = Person(**dict(valid_params, id=None))
50  
->>> p.validate()
51  
-{}
52  
->>> repr(p.id)
53  
-'None'
54  
-
55  
->>> p = Person(**dict(valid_params, is_child='t'))
56  
->>> p.validate()
57  
-{}
58  
->>> p.is_child
59  
-True
60  
-
61  
->>> p = Person(**dict(valid_params, is_child='f'))
62  
->>> p.validate()
63  
-{}
64  
->>> p.is_child
65  
-False
66  
-
67  
->>> p = Person(**dict(valid_params, is_child=True))
68  
->>> p.validate()
69  
-{}
70  
->>> p.is_child
71  
-True
72  
-
73  
->>> p = Person(**dict(valid_params, is_child=False))
74  
->>> p.validate()
75  
-{}
76  
->>> p.is_child
77  
-False
78  
-
79  
->>> p = Person(**dict(valid_params, is_child='foo'))
80  
->>> p.validate()['is_child']
81  
-[u'This value must be either True or False.']
82  
-
83  
->>> p = Person(**dict(valid_params, name=u'Jose'))
84  
->>> p.validate()
85  
-{}
86  
->>> p.name
87  
-u'Jose'
88  
-
89  
->>> p = Person(**dict(valid_params, name=227))
90  
->>> p.validate()
91  
-{}
92  
->>> p.name
93  
-u'227'
94  
-
95  
->>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
96  
->>> p.validate()
97  
-{}
98  
->>> p.birthdate
99  
-datetime.date(2000, 5, 3)
100  
-
101  
->>> p = Person(**dict(valid_params, birthdate=datetime.datetime(2000, 5, 3)))
102  
->>> p.validate()
103  
-{}
104  
->>> p.birthdate
105  
-datetime.date(2000, 5, 3)
106  
-
107  
->>> p = Person(**dict(valid_params, birthdate='2000-05-03'))
108  
->>> p.validate()
109  
-{}
110  
->>> p.birthdate
111  
-datetime.date(2000, 5, 3)
112  
-
113  
->>> p = Person(**dict(valid_params, birthdate='2000-5-3'))
114  
->>> p.validate()
115  
-{}
116  
->>> p.birthdate
117  
-datetime.date(2000, 5, 3)
118  
-
119  
->>> p = Person(**dict(valid_params, birthdate='foo'))
120  
->>> p.validate()['birthdate']
121  
-[u'Enter a valid date in YYYY-MM-DD format.']
122  
-
123  
->>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23)))
124  
->>> p.validate()
125  
-{}
126  
->>> p.favorite_moment
127  
-datetime.datetime(2002, 4, 3, 13, 23)
128  
-
129  
->>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3)))
130  
->>> p.validate()
131  
-{}
132  
->>> p.favorite_moment
133  
-datetime.datetime(2002, 4, 3, 0, 0)
134  
-
135  
->>> p = Person(**dict(valid_params, best_time='16:20:00'))
136  
->>> p.validate()
137  
-{}
138  
->>> p.best_time
139  
-datetime.time(16, 20)
140  
-
141  
->>> p = Person(**dict(valid_params, best_time='16:20'))
142  
->>> p.validate()
143  
-{}
144  
->>> p.best_time
145  
-datetime.time(16, 20)
146  
-
147  
->>> p = Person(**dict(valid_params, best_time='bar'))
148  
->>> p.validate()['best_time']
149  
-[u'Enter a valid time in HH:MM[:ss[.uuuuuu]] format.']
150  
-
151  
->>> p = Person(**dict(valid_params, email='john@example.com'))
152  
->>> p.validate()
153  
-{}
154  
->>> p.email
155  
-'john@example.com'
156  
-
157  
->>> p = Person(**dict(valid_params, email=u'john@example.com'))
158  
->>> p.validate()
159  
-{}
160  
->>> p.email
161  
-u'john@example.com'
162  
-
163  
->>> p = Person(**dict(valid_params, email=22))
164  
->>> p.validate()['email']
165  
-[u'Enter a valid e-mail address.']
166  
-
167  
-# Make sure that Date and DateTime return validation errors and don't raise Python errors.
168  
->>> p = Person(name='John Doe', is_child=True, email='abc@def.com')
169  
->>> errors = p.validate()
170  
->>> errors['favorite_moment']
171  
-[u'This field is required.']
172  
->>> errors['birthdate']
173  
-[u'This field is required.']
174  
->>> errors['best_time']
175  
-[u'This field is required.']
176  
-
177  
-"""}

0 notes on commit 788de6b

Please sign in to comment.
Something went wrong with that request. Please try again.