Class IS_IN_SET_LAZY
source code
gluon.validators.Validator --+
|
IS_IN_SET_LAZY
Like IS_IN_SET but with options obtained from a supplied function.
Options are instantiated when the validator or its options() method is
called, so don't need to be generated until it's used. Useful if the
field is not needed on every request, and does significant processing
to construct its options, or generates a large collection. If the
options are just from a database query, one can use IS_ONE_OF instead.
Raises an exception if an options collection is passed rather than a
callable as this is a programming error, e.g. accidentally *calling*
the options function in the constructor instead of passing the
function. That would not get lazy options instantiation.
The options collection (theset) and labels collection parameters to
IS_IN_SET are replaced by:
@param theset_fn: Function of no arguments that returns a collection
of options and (optionally) labels. Both options and labels can be
supplied via a dict or OrderedDict (options are keys, values are
labels), list (or tuple) of two-element lists (or tuples) (element 0 in
each pair is an option, element 1 is it's label). Otherwise, labels
are obtained either by calling the supplied represent function on each
item produced by theset_fn, or (if no represent is supplied), the items
themselves are used as labels.
@param represent: Function of one argument that returns the label for
a given option.
If there is a function call that returns the collection, just put
"lambda:" in front of the call. E.g.:
Field("nationality",
requires = IS_EMPTY_OR(IS_IN_SET_LAZY(
lambda: gis.get_countries(key_type="code"))),
label = T("Nationality"),
represent = lambda code: gis.get_country(code, key_type="code") or UNKNOWN_OPT)
Keyword parameters are same as for IS_IN_SET, except for labels, which
is not replaced by a function that parallels theset_fn, since ordering
is problematic if theset_fn returns a dict.
|
|
| __init__(self,
theset_fn,
represent=None,
error_message="value not allowed",
multiple=False,
zero="",
sort=False) |
source code
|
|
|
|
|
|
|
|
__init__(self,
theset_fn,
represent=None,
error_message="value not allowed",
multiple=False,
zero="",
sort=False)
(Constructor)
| source code
|
|