|Version 2 (modified by 14 years ago) ( diff ),|
The IS_ONE_OF() validator is a custom version of web2py's IS_IN_DB() for foreign key lookups in a key table. In forms, the respective field will be rendered as a drop-down field.
Other than IS_IN_DB, the IS_ONE_OF validator is deletion status sensitive, i.e. records in the key table that are flagged as 'deleted' will be ignored. Furthermore, it is possible to filter the key records by a type field (which should be int) for certain options.
db.table.field.requires = IS_NULL_OR(IS_ONE_OF(db, "keytable.keyfield", represent, filterby="fieldname", filter_opts=options))
- keytable = Name of the key table
- keyfield = Name of the key field
- represent = how to build the option label (in drop-downs in forms) from the key entry, one of the following:
- string template, e.g. "%(first_name)s %(last_name)s"
- list of names of fields, e.g. ('first_name', 'last_name')
- function or lambda, e.g. lambda r: "%(first_name)s %(last_name)s" % dict(r)
- filterby="fieldname" = the type field (and int-field) to filter for
- filter_opts="options" = a list or tuple of allowed values for the filterby-field
- Without represent and if no 'name' field can be detected in the table, the keyfield itself will be used as option label.
- If filterby is specified, the drop-down fields get sorted by this field.
... db.Field('pr_pe_id', db.pr_pentity), ... db[table].pr_pe_id.requires = IS_NULL_OR(IS_ONE_OF(db, 'pr_pentity.id', shn_pentity_represent, filterby='opt_pr_pentity_class', filter_opts=(1,) ))