Changeset 5237
- Timestamp:
- 05/14/07 11:24:51 (1 year ago)
- Files:
-
- django/trunk/django/contrib/formtools/preview.py (modified) (3 diffs)
- django/trunk/django/newforms/forms.py (modified) (4 diffs)
- django/trunk/django/newforms/models.py (modified) (3 diffs)
- django/trunk/docs/newforms.txt (modified) (7 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (6 diffs)
- django/trunk/tests/regressiontests/forms/regressions.py (modified) (1 diff)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/formtools/preview.py
r4265 r5237 25 25 Subclass FormPreview and define a done() method: 26 26 27 def done(self, request, clean _data):27 def done(self, request, cleaned_data): 28 28 # ... 29 29 … … 114 114 if self.security_hash(request, f) != request.POST.get(self.unused_name('hash')): 115 115 return self.failed_hash(request) # Security hash failed. 116 return self.done(request, f.clean _data)116 return self.done(request, f.cleaned_data) 117 117 else: 118 118 return render_to_response(self.form_template, … … 161 161 # METHODS SUBCLASSES MUST OVERRIDE ######################################## 162 162 163 def done(self, request, clean_data): 164 "Does something with the clean_data and returns an HttpResponseRedirect." 163 def done(self, request, cleaned_data): 164 """ 165 Does something with the cleaned_data and returns an 166 HttpResponseRedirect. 167 """ 165 168 raise NotImplementedError('You must define a done() method on your %s subclass.' % self.__class__.__name__) django/trunk/django/newforms/forms.py
r5231 r5237 170 170 def full_clean(self): 171 171 """ 172 Cleans all of self.data and populates self.__errors and self.clean _data.172 Cleans all of self.data and populates self.__errors and self.cleaned_data. 173 173 """ 174 174 errors = ErrorDict() … … 176 176 self.__errors = errors 177 177 return 178 self.clean _data = {}178 self.cleaned_data = {} 179 179 for name, field in self.fields.items(): 180 180 # value_from_datadict() gets the data from the dictionary. … … 184 184 try: 185 185 value = field.clean(value) 186 self.clean _data[name] = value187 if hasattr(self, ' do_clean_%s' % name):188 value = getattr(self, ' do_clean_%s' % name)()189 self.clean _data[name] = value186 self.cleaned_data[name] = value 187 if hasattr(self, 'clean_%s' % name): 188 value = getattr(self, 'clean_%s' % name)() 189 self.cleaned_data[name] = value 190 190 except ValidationError, e: 191 191 errors[name] = e.messages 192 192 try: 193 self.clean _data = self.clean()193 self.cleaned_data = self.clean() 194 194 except ValidationError, e: 195 195 errors[NON_FIELD_ERRORS] = e.messages 196 196 if errors: 197 delattr(self, 'clean _data')197 delattr(self, 'cleaned_data') 198 198 self.__errors = errors 199 199 … … 205 205 association with the field named '__all__'. 206 206 """ 207 return self.clean _data207 return self.cleaned_data 208 208 209 209 class Form(BaseForm): django/trunk/django/newforms/models.py
r5202 r5237 15 15 def save_instance(form, instance, fields=None, fail_message='saved', commit=True): 16 16 """ 17 Saves bound Form ``form``'s clean _data into model instance ``instance``.17 Saves bound Form ``form``'s cleaned_data into model instance ``instance``. 18 18 19 19 Assumes ``form`` has a field for every non-AutoField database field in … … 25 25 if form.errors: 26 26 raise ValueError("The %s could not be %s because the data didn't validate." % (opts.object_name, fail_message)) 27 clean _data = form.clean_data27 cleaned_data = form.cleaned_data 28 28 for f in opts.fields: 29 if not f.editable or isinstance(f, models.AutoField) or not f.name in clean _data:29 if not f.editable or isinstance(f, models.AutoField) or not f.name in cleaned_data: 30 30 continue 31 31 if fields and f.name not in fields: 32 32 continue 33 setattr(instance, f.name, clean _data[f.name])33 setattr(instance, f.name, cleaned_data[f.name]) 34 34 if commit: 35 35 instance.save() … … 37 37 if fields and f.name not in fields: 38 38 continue 39 if f.name in clean _data:40 setattr(instance, f.attname, clean _data[f.name])39 if f.name in cleaned_data: 40 setattr(instance, f.attname, cleaned_data[f.name]) 41 41 # GOTCHA: If many-to-many data is given and commit=False, the many-to-many 42 42 # data will be lost. This happens because a many-to-many options cannot be django/trunk/docs/newforms.txt
r5222 r5237 231 231 232 232 Once you've created a ``Form`` instance with a set of data and validated it, 233 you can access the clean data via the ``clean _data`` attribute of the ``Form``233 you can access the clean data via the ``cleaned_data`` attribute of the ``Form`` 234 234 object:: 235 235 … … 241 241 >>> f.is_valid() 242 242 True 243 >>> f.clean _data243 >>> f.cleaned_data 244 244 {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 245 245 … … 249 249 250 250 If your data does *not* validate, your ``Form`` instance will not have a 251 ``clean _data`` attribute::251 ``cleaned_data`` attribute:: 252 252 253 253 >>> data = {'subject': '', … … 258 258 >>> f.is_valid() 259 259 False 260 >>> f.clean _data260 >>> f.cleaned_data 261 261 Traceback (most recent call last): 262 262 ... 263 AttributeError: 'ContactForm' object has no attribute 'clean _data'264 265 ``clean _data`` will always *only* contain a key for fields defined in the263 AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 264 265 ``cleaned_data`` will always *only* contain a key for fields defined in the 266 266 ``Form``, even if you pass extra data when you define the ``Form``. In this 267 267 example, we pass a bunch of extra fields to the ``ContactForm`` constructor, 268 but ``clean _data`` contains only the form's fields::268 but ``cleaned_data`` contains only the form's fields:: 269 269 270 270 >>> data = {'subject': 'hello', … … 278 278 >>> f.is_valid() 279 279 True 280 >>> f.clean _data # Doesn't contain extra_field_1, etc.280 >>> f.cleaned_data # Doesn't contain extra_field_1, etc. 281 281 {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 282 282 283 ``clean _data`` will include a key and value for *all* fields defined in the283 ``cleaned_data`` will include a key and value for *all* fields defined in the 284 284 ``Form``, even if the data didn't include a value for fields that are not 285 285 required. In this example, the data dictionary doesn't include a value for the 286 ``nick_name`` field, but ``clean _data`` includes it, with an empty value::286 ``nick_name`` field, but ``cleaned_data`` includes it, with an empty value:: 287 287 288 288 >>> class OptionalPersonForm(Form): … … 294 294 >>> f.is_valid() 295 295 True 296 >>> f.clean _data296 >>> f.cleaned_data 297 297 {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 298 298 299 In this above example, the ``clean _data`` value for ``nick_name`` is set to an299 In this above example, the ``cleaned_data`` value for ``nick_name`` is set to an 300 300 empty string, because ``nick_name`` is ``CharField``, and ``CharField``\s treat 301 301 empty values as an empty string. Each field type knows what its "blank" value … … 309 309 310 310 >>> f = ContactForm() 311 >>> f.clean _data311 >>> f.cleaned_data 312 312 Traceback (most recent call last): 313 313 ... 314 AttributeError: 'ContactForm' object has no attribute 'clean _data'314 AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 315 315 316 316 Outputting forms as HTML django/trunk/tests/modeltests/model_forms/models.py
r5202 r5237 19 19 20 20 The function django.newforms.save_instance() takes a bound form instance and a 21 model instance and saves the form's clean _data into the instance. It also takes21 model instance and saves the form's cleaned_data into the instance. It also takes 22 22 a commit=True parameter. 23 23 """ … … 95 95 >>> f.is_valid() 96 96 True 97 >>> f.clean _data97 >>> f.cleaned_data 98 98 {'url': u'entertainment', 'name': u'Entertainment'} 99 99 >>> obj = f.save() … … 106 106 >>> f.is_valid() 107 107 True 108 >>> f.clean _data108 >>> f.cleaned_data 109 109 {'url': u'test', 'name': u"It's a test"} 110 110 >>> obj = f.save() … … 120 120 >>> f.is_valid() 121 121 True 122 >>> f.clean _data122 >>> f.cleaned_data 123 123 {'url': u'third', 'name': u'Third test'} 124 124 >>> obj = f.save(commit=False) … … 135 135 >>> f.errors 136 136 {'name': [u'This field is required.']} 137 >>> f.clean _data138 Traceback (most recent call last): 139 ... 140 AttributeError: 'CategoryForm' object has no attribute 'clean _data'137 >>> f.cleaned_data 138 Traceback (most recent call last): 139 ... 140 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data' 141 141 >>> f.save() 142 142 Traceback (most recent call last): … … 525 525 >>> f.is_valid() 526 526 True 527 >>> f.clean _data527 >>> f.cleaned_data 528 528 {'phone': u'312-555-1212', 'description': u'Assistance'} 529 529 """} django/trunk/tests/regressiontests/forms/regressions.py
r5231 r5237 47 47 >>> f.is_valid() 48 48 True 49 >>> f.clean _data49 >>> f.cleaned_data 50 50 {'data': u'xyzzy'} 51 51 """ django/trunk/tests/regressiontests/forms/tests.py
r5231 r5237 1775 1775 >>> p.errors.as_text() 1776 1776 u'' 1777 >>> p.clean _data1777 >>> p.cleaned_data 1778 1778 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1779 1779 >>> print p['first_name'] … … 1811 1811 >>> p.is_valid() 1812 1812 False 1813 >>> p.clean _data1814 Traceback (most recent call last): 1815 ... 1816 AttributeError: 'Person' object has no attribute 'clean _data'1813 >>> p.cleaned_data 1814 Traceback (most recent call last): 1815 ... 1816 AttributeError: 'Person' object has no attribute 'cleaned_data' 1817 1817 >>> print p 1818 1818 <tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr> … … 1845 1845 >>> p.is_valid() 1846 1846 False 1847 >>> p.clean _data1848 Traceback (most recent call last): 1849 ... 1850 AttributeError: 'Person' object has no attribute 'clean _data'1847 >>> p.cleaned_data 1848 Traceback (most recent call last): 1849 ... 1850 AttributeError: 'Person' object has no attribute 'cleaned_data' 1851 1851 >>> print p 1852 1852 <tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> … … 1887 1887 * birthday 1888 1888 * This field is required. 1889 >>> p.clean _data1890 Traceback (most recent call last): 1891 ... 1892 AttributeError: 'Person' object has no attribute 'clean _data'1889 >>> p.cleaned_data 1890 Traceback (most recent call last): 1891 ... 1892 AttributeError: 'Person' object has no attribute 'cleaned_data' 1893 1893 >>> p['first_name'].errors 1894 1894 [u'This field is required.'] … … 1906 1906 <input type="text" name="birthday" id="id_birthday" /> 1907 1907 1908 clean _data will always *only* contain a key for fields defined in the1908 cleaned_data will always *only* contain a key for fields defined in the 1909 1909 Form, even if you pass extra data when you define the Form. In this 1910 1910 example, we pass a bunch of extra fields to the form constructor, 1911 but clean _data contains only the form's fields.1911 but cleaned_data contains only the form's fields. 1912 1912 >>> data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'} 1913 1913 >>> p = Person(data) 1914 1914 >>> p.is_valid() 1915 1915 True 1916 >>> p.clean _data1916 >>> p.cleaned_data 1917 1917 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1918 1918 1919 clean _data will include a key and value for *all* fields defined in the Form,1919 cleaned_data will include a key and value for *all* fields defined in the Form, 1920 1920 even if the Form's data didn't include a value for fields that are not 1921 1921 required. In this example, the data dictionary doesn't include a value for the 1922 "nick_name" field, but clean _data includes it. For CharFields, it's set to the1922 "nick_name" field, but cleaned_data includes it. For CharFields, it's set to the 1923 1923 empty string. 1924 1924 >>> class OptionalPersonForm(Form): … … 1930 1930 >>> f.is_valid() 1931 1931 True 1932 >>> f.clean _data1932 >>> f.cleaned_data 1933 1933 {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 1934 1934 … … 1942 1942 >>> f.is_valid() 1943 1943 True 1944 >>> f.clean _data1944 >>> f.cleaned_data 1945 1945 {'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} 1946 1946 … … 2293 2293 >>> f.errors 2294 2294 {} 2295 >>> f.clean _data2295 >>> f.cleaned_data 2296 2296 {'composers': [u'J'], 'name': u'Yesterday'} 2297 2297 >>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False) 2298 2298 >>> f.errors 2299 2299 {} 2300 >>> f.clean _data2300 >>> f.cleaned_data 2301 2301 {'composers': [u'J', u'P'], 'name': u'Yesterday'} 2302 2302 … … 2304 2304 >>> class EscapingForm(Form): 2305 2305 ... special_name = CharField() 2306 ... def do_clean_special_name(self):2307 ... raise ValidationError("Something's wrong with '%s'" % self.clean _data['special_name'])2306 ... def clean_special_name(self): 2307 ... raise ValidationError("Something's wrong with '%s'" % self.cleaned_data['special_name']) 2308 2308 2309 2309 >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) … … 2320 2320 clean_XXX() method on the Form, where XXX is the field name. As in 2321 2321 Field.clean(), the clean_XXX() method should return the cleaned value. In the 2322 clean_XXX() method, you have access to self.clean _data, which is a dictionary2322 clean_XXX() method, you have access to self.cleaned_data, which is a dictionary 2323 2323 of all the data that has been cleaned *so far*, in order by the fields, 2324 2324 including the current field (e.g., the field XXX if you're in clean_XXX()). … … 2327 2327 ... password1 = CharField(widget=PasswordInput) 2328 2328 ... password2 = CharField(widget=PasswordInput) 2329 ... def do_clean_password2(self):2330 ... if self.clean _data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']:2329 ... def clean_password2(self): 2330 ... if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 2331 2331 ... raise ValidationError(u'Please make sure your passwords match.') 2332 ... return self.clean _data['password2']2332 ... return self.cleaned_data['password2'] 2333 2333 >>> f = UserRegistration(auto_id=False) 2334 2334 >>> f.errors … … 2343 2343 >>> f.errors 2344 2344 {} 2345 >>> f.clean _data2345 >>> f.cleaned_data 2346 2346 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 2347 2347 … … 2350 2350 method will not be associated with a particular field; it will have a 2351 2351 special-case association with the field named '__all__'. 2352 Note that in Form.clean(), you have access to self.clean _data, a dictionary of2352 Note that in Form.clean(), you have access to self.cleaned_data, a dictionary of 2353 2353 all the fields/values that have *not* raised a ValidationError. Also note 2354 2354 Form.clean() is required to return a dictionary of all clean data. … … 2358 2358 ... password2 = CharField(widget=PasswordInput) 2359 2359 ... def clean(self): 2360 ... if self.clean _data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']:2360 ... if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 2361 2361 ... raise ValidationError(u'Please make sure your passwords match.') 2362 ... return self.clean _data2362 ... return self.cleaned_data 2363 2363 >>> f = UserRegistration(auto_id=False) 2364 2364 >>> f.errors … … 2387 2387 >>> f.errors 2388 2388 {} 2389 >>> f.clean _data2389 >>> f.cleaned_data 2390 2390 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 2391 2391 … … 2955 2955 >>> p.is_valid() 2956 2956 True 2957 >>> p.clean _data2957 >>> p.cleaned_data 2958 2958 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 2959 2959 … … 2999 2999 >>> p1.is_valid() 3000 3000 True 3001 >>> p1.clean _data3001 >>> p1.cleaned_data 3002 3002 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 3003 3003 >>> p2 = Person(data, prefix='person2') 3004 3004 >>> p2.is_valid() 3005 3005 True 3006 >>> p2.clean _data3006 >>> p2.cleaned_data 3007 3007 {'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} 3008 3008 … … 3030 3030 >>> p.is_valid() 3031 3031 True 3032 >>> p.clean _data3032 >>> p.cleaned_data 3033 3033 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 3034 3034 … … 3092 3092 ... password2 = CharField(widget=PasswordInput) 3093 3093 ... def clean(self): 3094 ... if self.clean _data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']:3094 ... if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 3095 3095 ... raise ValidationError(u'Please make sure your passwords match.') 3096 ... return self.clean _data3096 ... return self.cleaned_data 3097 3097 >>> def my_function(method, post_data): 3098 3098 ... if method == 'POST': … … 3101 3101 ... form = UserRegistration(auto_id=False) 3102 3102 ... if form.is_valid(): 3103 ... return 'VALID: %r' % form.clean _data3103 ... return 'VALID: %r' % form.cleaned_data 3104 3104 ... t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>') 3105 3105 ... return t.render(Context({'form': form})) … … 3139 3139 ... password2 = CharField(widget=PasswordInput) 3140 3140 ... def clean(self): 3141 ... if self.clean _data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']:3141 ... if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 3142 3142 ... raise ValidationError(u'Please make sure your passwords match.') 3143 ... return self.clean _data3143 ... return self.cleaned_data 3144 3144 3145 3145 You have full flexibility in displaying form fields in a template. Just pass a … … 3491 3491 <input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr> 3492 3492 3493 >>> f.clean _data3493 >>> f.cleaned_data 3494 3494 {'field1': u'some text,JP,2007-04-25 06:24:00'} 3495 3495
