Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions baph/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

orm = ORM.get()


def save_instance(form, instance, fields=None, fail_message='saved',
commit=True, exclude=None):
"""
Expand All @@ -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))
Expand Down