summaryrefslogtreecommitdiff
path: root/postgresqleu/util/backendviews.py
diff options
context:
space:
mode:
authorMagnus Hagander2023-08-02 09:35:37 +0000
committerMagnus Hagander2023-08-02 13:06:31 +0000
commitc79109da39eb78041a3b7765dcefa607d5c03aaa (patch)
tree825c962c0f9030971b455d3e4ed733116b8ea7aa /postgresqleu/util/backendviews.py
parentc187ca962b2c73f0478124244cae28b59c5997cb (diff)
Support deleting keys in json_form_fields
If the attribute delete_on_empty is set to True on the Field, and the value submitted from the user is empty, delete the key from the json object instead of setting it to the empty string. The default behaviour remains the previous one, which is set the key explicitly to the empty string.
Diffstat (limited to 'postgresqleu/util/backendviews.py')
-rw-r--r--postgresqleu/util/backendviews.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/postgresqleu/util/backendviews.py b/postgresqleu/util/backendviews.py
index e1eb614b..685320d9 100644
--- a/postgresqleu/util/backendviews.py
+++ b/postgresqleu/util/backendviews.py
@@ -181,7 +181,13 @@ def backend_process_form(request, urlname, formclass, id, cancel_url='../', save
if form.json_form_fields:
for fn, ffields in form.json_form_fields.items():
d = getattr(form.instance, fn, {})
- d.update({fld: form.cleaned_data[fld] for fld in ffields})
+ for fld in ffields:
+ if form.cleaned_data[fld] or not getattr(form.fields[fld], 'delete_on_empty', False):
+ # If we have a value, or if we're asked to store empty strings,
+ # then do so.
+ d[fld] = form.cleaned_data[fld]
+ elif form.cleaned_data[fld] == '' and getattr(form.fields[fld], 'delete_on_empty', False):
+ del d[fld]
setattr(form.instance, fn, d)
form.instance.save(update_fields=form.json_form_fields.keys())