def save_instance(form, instance, fields=None, fail_message='saved',
                  commit=True, exclude=None):
    if form.errors:
        raise ValueError("The %s could not be %s because the data didn't"
                " validate." % ('object', fail_message))

    cleaned_data = form.cleaned_data

    for field_name, field_type in instance.structure.items():
        if fields and field_name not in fields:
            continue
        if exclude and field_name in exclude:
            continue

        instance[field_name] = cleaned_data[field_name]

    if commit:
        instance.save(validate=True)

    return instance
dataset/ETHPy150Open peterbe/django-mongokit/django_mongokit/forms/forms.py/save_instance/original