Ticket #8672: one-more.diff
File one-more.diff, 4.8 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
21 21 ) 22 22 23 23 def save_instance(form, instance, fields=None, fail_message='saved', 24 commit=True ):24 commit=True, force_insert=False, force_update=False): 25 25 """ 26 26 Saves bound Form ``form``'s cleaned_data into model instance ``instance``. 27 27 … … 52 52 f.save_form_data(instance, cleaned_data[f.name]) 53 53 if commit: 54 54 # If we are committing, save the instance and the m2m data immediately. 55 instance.save( )55 instance.save(force_insert=force_insert, force_update=force_update) 56 56 save_m2m() 57 57 else: 58 58 # We're not committing. Add a method to the form to allow deferred … … 62 62 63 63 def make_model_save(model, fields, fail_message): 64 64 """Returns the save() method for a Form.""" 65 def save(self, commit=True): 66 return save_instance(self, model(), fields, fail_message, commit) 65 def save(self, commit=True, force_insert=True, force_update=False): 66 return save_instance(self, model(), fields, fail_message, 67 commit, force_insert, force_update) 67 68 return save 68 69 69 70 def make_instance_save(instance, fields, fail_message): 70 71 """Returns the save() method for a Form.""" 71 def save(self, commit=True): 72 return save_instance(self, instance, fields, fail_message, commit) 72 def save(self, commit=True, force_insert=False, force_update=True): 73 return save_instance(self, instance, fields, fail_message, 74 commit, force_insert, force_update) 73 75 return save 74 76 75 77 def form_for_fields(field_list): … … 203 205 super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data, 204 206 error_class, label_suffix, empty_permitted) 205 207 206 def save(self, commit=True ):208 def save(self, commit=True, force_insert=False, force_update=False): 207 209 """ 208 210 Saves this ``form``'s cleaned_data into model instance 209 211 ``self.instance``. … … 211 213 If commit=True, then the changes to ``instance`` will be saved to the 212 214 database. Returns ``instance``. 213 215 """ 216 # Try to make fail message more helpful 217 # (actually only correct if pk is an AutoField) 214 218 if self.instance.pk is None: 215 219 fail_message = 'created' 216 220 else: 217 221 fail_message = 'changed' 218 return save_instance(self, self.instance, self._meta.fields, fail_message, commit) 222 return save_instance(self, self.instance, self._meta.fields, fail_message, 223 commit, force_insert, force_update) 219 224 220 225 class ModelForm(BaseModelForm): 221 226 __metaclass__ = ModelFormMetaclass … … 261 266 262 267 def save_new(self, form, commit=True): 263 268 """Saves and returns a new model instance for the given form.""" 264 return save_instance(form, self.model(), commit=commit )269 return save_instance(form, self.model(), commit=commit, force_insert=True) 265 270 266 271 def save_existing(self, form, instance, commit=True): 267 272 """Saves and returns an existing model instance for the given form.""" 268 return save_instance(form, instance, commit=commit )273 return save_instance(form, instance, commit=commit, force_update=True) 269 274 270 275 def save(self, commit=True): 271 276 """Saves model instances for every form, adding and changing instances 272 277 as necessary, and returns the list of instances. 273 278 """ 279 # force_insert and force_update flags not supported, since the FormSet will 280 # do a mixture of these on existing and new objects 274 281 if not commit: 275 282 self.saved_forms = [] 276 283 def save_m2m(): … … 369 376 def save_new(self, form, commit=True): 370 377 kwargs = {self.fk.get_attname(): self.instance.pk} 371 378 new_obj = self.model(**kwargs) 372 return save_instance(form, new_obj, commit=commit )379 return save_instance(form, new_obj, commit=commit, force_insert=True) 373 380 374 381 def _get_foreign_key(parent_model, model, fk_name=None): 375 382 """ -
django/contrib/contenttypes/generic.py
324 324 self.ct_fk_field.get_attname(): self.instance.pk, 325 325 } 326 326 new_obj = self.model(**kwargs) 327 return save_instance(form, new_obj, commit=commit )327 return save_instance(form, new_obj, commit=commit, force_insert=True) 328 328 329 329 def generic_inlineformset_factory(model, form=ModelForm, 330 330 formset=BaseGenericInlineFormSet,