squirrels.parameters or the squirrels module.
Factory methods
Factory methods are class methods that create and configure parameter instances. These methods are typically used in thepyconfigs/parameters.py file to create the parameter configurations (which describes the “shape” of the parameter but does not include the realtime user selections).
create_simple()
Decorator for creating a simple parameter from a function that returns select options. The decorated function must return a list of SelectParameterOption objects.create_with_options()
Decorator for creating a parameter with options that can vary based on user attributes or parent parameter selections. The decorated function must return a list of SelectParameterOption objects. This is functionally equivalent tocreate_simple but with additional arguments available for user_attribute and parent_name.
create_from_source()
Decorator for creating a parameter populated from a database table or query using a SelectDataSource. The decorated function must return a SelectDataSource object.Instance methods
Instance methods are available on parameter instances at query time (incontext.py or data models) to retrieve selected values.
get_selected() -> SelectParameterOption
Gets the selected SelectParameterOption object.SelectParameterOption | None
The selected SelectParameterOption object (or None if the parameter has no selectable options).
get_selected() -> Any | None
Gets the custom field from the selected option.Any | None
The custom field value, or None if either:
- the field does not exist and no default is provided, or
- the parameter has no selectable options.
get_selected_id()
Gets the ID of the selected option.str | None
The ID string of the selected option (or None if the parameter has no selectable options).
is_enabled()
Returns whether the parameter is enabled. A parameter is enabled if it has at least one selectable option.bool
True if the parameter has at least one selectable option, False otherwise.
Examples for factory methods
All examples below are defined in thepyconfigs/parameters.py file.
Using create_simple for basic dropdowns
For parameters that don’t need user-specific or parent-dependent options, you can usecreate_simple.
Cascading dropdowns with parent-child relationship
This example shows how to create dependent dropdowns where the child options change based on the parent selection.User-specific options
This example restricts certain options based on user access levels.pyconfigs/user.py, then they can be used to restrict visibility of parameter options as well. To do so, the user_attribute argument must be prefixed with custom_fields..
Single-select from database source
This example populates dropdown options from a database table.Cascading dropdown from database with parent
This example shows cascading from a database where products depend on the selected category.Examples for instance methods
Once parameters are configured, you can use instance methods in your models to access the selected values. The parameter instances are available through the context object (e.g.,sqrl.prms).
Basic usage in context.py
Basic usage in Jinja SQL models
The following example works but is not recommended. See tip below for why.Accessing custom fields
Suppose you define a single-select parameter with custom fields as such:context.py as follows: