Ticket #5153: ticket5153.diff
File ticket5153.diff, 2.5 KB (added by , 17 years ago) |
---|
-
django/newforms/models.py
29 29 continue 30 30 form.initial[f.name] = f.value_from_object(instance) 31 31 32 def save_instance(form, instance, fields=None, fail_message='saved', commit=True): 32 def save_instance(form, instance, fields=None, fail_message='saved', commit=True, 33 ensure_valid=True): 33 34 """ 34 35 Saves bound Form ``form``'s cleaned_data into model instance ``instance``. 35 36 36 37 If commit=True, then the changes to ``instance`` will be saved to the 37 database. Returns ``instance``. 38 database. If ensure_valid=False, the changes will be saved even if the form 39 has validation errors. 40 41 Returns ``instance``. 38 42 """ 39 43 from django.db import models 40 44 opts = instance.__class__._meta 41 if form.errors: 45 if not ensure_valid: 46 form.full_clean(ensure_valid=False) 47 elif form.errors: 42 48 raise ValueError("The %s could not be %s because the data didn't validate." % (opts.object_name, fail_message)) 43 49 cleaned_data = form.cleaned_data 44 50 for f in opts.fields: -
django/newforms/forms.py
170 170 """ 171 171 return self.errors.get(NON_FIELD_ERRORS, ErrorList()) 172 172 173 def full_clean(self ):173 def full_clean(self, ensure_valid=True): 174 174 """ 175 175 Cleans all of self.data and populates self._errors and 176 176 self.cleaned_data. 177 178 If ensure_valid=True, the self.cleaned_data attribute is not set if 179 validation errors are encountered. 177 180 """ 178 181 self._errors = ErrorDict() 179 182 if not self.is_bound: # Stop further processing. … … 192 195 self.cleaned_data[name] = value 193 196 except ValidationError, e: 194 197 self._errors[name] = e.messages 195 if name in self.cleaned_data:198 if ensure_valid and name in self.cleaned_data: 196 199 del self.cleaned_data[name] 197 200 try: 198 201 self.cleaned_data = self.clean() 199 202 except ValidationError, e: 200 203 self._errors[NON_FIELD_ERRORS] = e.messages 201 if self._errors:204 if ensure_valid and self._errors: 202 205 delattr(self, 'cleaned_data') 203 206 204 207 def clean(self):