def save_instance(self, instance, fields=None, fail_message='saved',
                      commit=True, exclude=None, construct=True):
        """
        Saves bound Form ``form``'s cleaned_data into model instance
        ``instance``.

        If commit=True, then the changes to ``instance`` will be saved to the
        database. Returns ``instance``.

        If construct=False, assume ``instance`` has already been constructed
        and just needs to be saved.
        """
        if construct:
            instance = construct_instance(self, instance, fields, exclude)
        opts = instance._meta
        if self.errors:
            raise ValueError("the %s could not be %s because the data didn't"
                             " validate." % (opts.object_name, fail_message))

        # wrap up the saving of m2m data as a function.
        def save_m2m(instance):
            cleaned_data = self.cleaned_data
            for f in opts.many_to_many:
                if fields and f.name not in fields:
                    continue
                if f.name in cleaned_data:
                    for validator in f.validators:
                        validator(instance, cleaned_data[f.name])
                    f.save_form_data(instance, cleaned_data[f.name])
        if commit:
            # TODO XXX, why are we not using transactions?
            # if we are committing, save the instance and the m2m data
            # immediately.
            if not instance.pk:
                rollback = True
            else:
                rollback = False
            instance.save()
            try:
                save_m2m(instance)
                # ^-- pass instance so we can validate it's views
            except __HOLE__:
                if rollback:
                    self.delete_instance(instance)
                    # we didn't call ensure_label_domain so it's not our
                    # responsibility to call prune_tree
                raise
        else:
            # we're not committing. add a method to the form to allow deferred
            # saving of m2m data.
            self.save_m2m = save_m2m
        return instance
dataset/ETHPy150Open mozilla/inventory/mozdns/forms.py/BaseForm.save_instance