Ticket #8672: flags-for-modelform-save.diff

File flags-for-modelform-save.diff, 4.2 KB (added by Richard Davies <richard.davies@…>, 11 years ago)
  • forms/models.py

     
    2121)
    2222
    2323def save_instance(form, instance, fields=None, fail_message='saved',
    24                   commit=True):
     24                  commit=True, force_insert=False, force_update=False):
    2525    """
    2626    Saves bound Form ``form``'s cleaned_data into model instance ``instance``.
    2727
     
    5252                f.save_form_data(instance, cleaned_data[f.name])
    5353    if commit:
    5454        # 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)
    5656        save_m2m()
    5757    else:
    5858        # We're not committing. Add a method to the form to allow deferred
     
    6262
    6363def make_model_save(model, fields, fail_message):
    6464    """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)
    6768    return save
    6869
    6970def make_instance_save(instance, fields, fail_message):
    7071    """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)
    7375    return save
    7476
    7577def form_for_fields(field_list):
     
    203205        super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data,
    204206                                            error_class, label_suffix, empty_permitted)
    205207
    206     def save(self, commit=True):
     208    def save(self, commit=True, force_insert=False, force_update=False):
    207209        """
    208210        Saves this ``form``'s cleaned_data into model instance
    209211        ``self.instance``.
     
    211213        If commit=True, then the changes to ``instance`` will be saved to the
    212214        database. Returns ``instance``.
    213215        """
     216        # Try to make fail message more helpful
     217        # (actually only correct if pk is an AutoField)       
    214218        if self.instance.pk is None:
    215219            fail_message = 'created'
    216220        else:
    217221            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)
    219224
    220225class ModelForm(BaseModelForm):
    221226    __metaclass__ = ModelFormMetaclass
     
    261266
    262267    def save_new(self, form, commit=True):
    263268        """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)
    265270
    266271    def save_existing(self, form, instance, commit=True):
    267272        """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)
    269274
    270275    def save(self, commit=True):
    271276        """Saves model instances for every form, adding and changing instances
    272277        as necessary, and returns the list of instances.
    273278        """
     279        # force_insert and force_update flags not supported, since the FormSet will
     280        # do a mixture of these on existing and new objects
    274281        if not commit:
    275282            self.saved_forms = []
    276283            def save_m2m():
     
    369376    def save_new(self, form, commit=True):
    370377        kwargs = {self.fk.get_attname(): self.instance.pk}
    371378        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)
    373380
    374381def _get_foreign_key(parent_model, model, fk_name=None):
    375382    """
Back to Top