diff --git a/baph/forms/forms.py b/baph/forms/forms.py index cc9fcdd..8c3e4fb 100644 --- a/baph/forms/forms.py +++ b/baph/forms/forms.py @@ -39,6 +39,7 @@ orm = ORM.get() + def save_instance(form, instance, fields=None, fail_message='saved', commit=True, exclude=None): """ @@ -48,16 +49,23 @@ def save_instance(form, instance, fields=None, fail_message='saved', database. Returns ``instance``. """ opts = instance._meta - for k,v in form.cleaned_data.items(): - if k in form.data: - try: - # TODO: this fails when trying to reach the remote side - # of an association_proxy when the interim node is None - # find a better solution - setattr(instance, k, v) - except TypeError as e: - continue - + for k, v in form.cleaned_data.items(): + if k not in form.data: + # ignore values that weren't submitted + continue + current = getattr(instance, k, None) + if current == v: + # value is unchanged + continue + + try: + # TODO: this fails when trying to reach the remote side + # of an association_proxy when the interim node is None + # find a better solution + setattr(instance, k, v) + except TypeError: + continue + if form.errors: raise ValueError("The %s could not be %s because the data didn't" " validate." % (opts.object_name, fail_message))