Ignore fields which are not included on the form (dynamic rendering)

This commit is contained in:
Jeremy Stretch 2024-03-13 09:43:01 -04:00
parent 4c7b6fcec0
commit 33b9ebb201

View File

@ -54,17 +54,24 @@ def render_fieldset(form, fieldset, heading=None):
""" """
rows = [] rows = []
for item in fieldset: for item in fieldset:
# Multiple fields side-by-side
if type(item) is InlineFields: if type(item) is InlineFields:
fields = [
form[name] for name in item.field_names if name in form.fields
]
rows.append( rows.append(
('inline', item.label, [form[name] for name in item.field_names]) ('inline', item.label, fields)
) )
# Tabbed groups of fields
elif type(item) is TabbedFieldGroups: elif type(item) is TabbedFieldGroups:
tabs = [ tabs = [
{ {
'id': tab['id'], 'id': tab['id'],
'title': tab['title'], 'title': tab['title'],
'active': bool(form.initial.get(tab['fields'][0], False)), 'active': bool(form.initial.get(tab['fields'][0], False)),
'fields': [form[name] for name in tab['fields']] 'fields': [form[name] for name in tab['fields'] if name in form.fields]
} for tab in item.tabs } for tab in item.tabs
] ]
# If none of the tabs has been marked as active, activate the first one # If none of the tabs has been marked as active, activate the first one
@ -73,7 +80,9 @@ def render_fieldset(form, fieldset, heading=None):
rows.append( rows.append(
('tabs', None, tabs) ('tabs', None, tabs)
) )
else:
# A single form field
elif item in form.fields:
rows.append( rows.append(
('field', None, [form[item]]) ('field', None, [form[item]])
) )