Skip to content

Instantly share code, notes, and snippets.

Created August 31, 2012 19:32
Show Gist options
  • Save anonymous/ff543a5c6d3bc14b9079 to your computer and use it in GitHub Desktop.
Save anonymous/ff543a5c6d3bc14b9079 to your computer and use it in GitHub Desktop.
web2py patch allowing for queries on 1:M relationships
--- a/gluon/sqlhtml.py
+++ b/gluon/sqlhtml.py
@@ -1335,7 +1335,9 @@ class SQLFORM(FORM):
for field in fields:
name = str(field).replace('.','-')
criterion = []
+ is_reference = field.type.startswith('reference')
options = search_options.get(field.type,None)
+ if is_reference: options = ['=']
if options:
label = isinstance(field.label,str) and T(field.label) or field.label
selectfields.append((str(field),label))
@@ -1344,6 +1346,22 @@ class SQLFORM(FORM):
value_input = SELECT(
OPTION(T("True"),_value="T"),OPTION(T("False"),_value="F"),
_id="w2p_value_"+name)
+ elif is_reference:
+ reference_table = field.db[field.type[10:]]
+
+ # Finding all records for lookup may be very expensive!
+ # Need to implement something where this is opt-in, not
+ # enabled by default in GRID or SMARTGRID.
+ all_rows = field.db().select(reference_table.ALL)
+
+ options = []
+ for row in all_rows:
+ if reference_table._format:
+ record_name = reference_table._format % row
+ else:
+ record_name = str(row['id'])
+ options.append(OPTION(T(record_name), _value=row.id))
+ value_input = SELECT(options, _id="w2p_value_"+name)
else:
value_input = INPUT(_type='text',_id="w2p_value_"+name,_class=field.type)
new_button = INPUT(_type="button", _value=T('New'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment