Skip to content

Commit 960f882

Browse files
committed
allow forms without Fields, thanks dgmanns
1 parent 2c6847f commit 960f882

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

py4web/utils/form.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,25 +1032,31 @@ def __init__(
10321032
)
10331033

10341034
if isinstance(table, list):
1035-
if len(table) == 0:
1035+
fields_list = table
1036+
if len(fields_list) == 0:
10361037
raise ValueError("Cannot build form with empty list of fields")
10371038
# using _table to check if Field.bind was called
10381039
# and the field is bound to a table, since unlike `tablename`, `_table` is
10391040
# only set in Field.bind()
10401041
all_tablenames = list(
10411042
set(
1042-
str(getattr(field, "_table", None) or "no_table") for field in table
1043+
str(getattr(field, "_table", None) or "no_table")
1044+
for field in fields_list
10431045
)
10441046
)
10451047

1046-
# only disable dbio if the fields are from multiple tables
1048+
# disable dbio if there are no fields or the fields are from multiple tables
10471049
# this allows making forms for a subset of fields easily:
10481050
# Form([db.tbl.field1, db.tbl.field2])
1049-
if len(all_tablenames) > 1 or all_tablenames[0] in "no_table":
1051+
if (
1052+
not all_tablenames
1053+
or all_tablenames[0] in "no_table"
1054+
or len(all_tablenames) > 1
1055+
):
10501056
dbio = False
10511057
# Mimic a table from a list of fields without calling define_table
10521058
form_name = form_name or "no_table"
1053-
for field in table:
1059+
for field in fields_list:
10541060
field.tablename = getattr(field, "tablename", form_name)
10551061
else:
10561062
# if we just have 1 table, use it as the form name

0 commit comments

Comments
 (0)