FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 665495f0 authored by Dr Catherine Pitt's avatar Dr Catherine Pitt
Browse files

Allow non-integer key for null value in hid choices

The database module has a get_hids() function that generates a list of
2-element lists representing a list of foreign key values and their
human-readable equivalents from the database. These are used to present
select lists for post categories, nationalities and the like. Generally
the keys are integers. The function has an option to add a "null" option
to the list, for use in selects where input is optional. The value
associated with that defaulted to the integer -1.

This became a problem when I needed to add a select field with string
keys (post category ids) and also having a null option, because the null
option was coerced from an int to a string like the rest of the data, so
always failed validation.  This change makes the value for the null
option configurable but defaulting to -1.
parent 97380268
No related branches found
No related tags found
No related merge requests found
......@@ -31,11 +31,12 @@ def get_db():
return chemdb
def get_hids(tablestem, null_option=None):
def get_hids(tablestem, null_option=None, null_value=-1):
"""
Get a list of human-readable-identifier, primary key pairs from the database for a table
null_option adds an option which translates to null in the database
null_value is the value to set for it
"""
tablename = tablestem + "_hid"
db = get_db()
......@@ -46,8 +47,8 @@ def get_hids(tablestem, null_option=None):
hids = db.cursor.fetchall()
if null_option is not None:
# We need a 'no data' value. 'None' is easily confused with Python None,
# but NULL implies SQL NULL; compromising on -1
hids.insert(0, (-1, null_option))
# but NULL implies SQL NULL; compromising on -1 as the default for null_value
hids.insert(0, (null_value, null_option))
db.conn.rollback()
return hids
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment