From 7806b11539c3c838e100a229fdc29b5f4e670b1b Mon Sep 17 00:00:00 2001 From: Daniel Rust Date: Thu, 12 Sep 2019 14:00:19 -0700 Subject: [PATCH] (devhub/dhplatform#8780) updates to save_instance --- baph/forms/forms.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) 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))