This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.
Business logic often requires your forms to adapt to their context. Forest Admin makes this possible through a powerful way to extend your form's logic.
To make an action form dynamic, simply use functions instead of a static value on the compatible properties.
Note that:
Both synchronous and asynchronous functions are supported
As such, they have access to the current values of the form.
And the records that the action will be applied to.
Form field properties
functions can be used for the following properties which are also available as static values:
Property
Description
is_required
Make the field required.
is_read_only
Make the field read-only.
default_value
Set the default value of the field.
In addition, depending on the field type, you can also use functions for the following properties:
Property
Description
enum_values
Change the list of possible values of the field when type == 'Enum'.
collection_name
Change the target collection of the field when type: 'Collection'.
And finally, those two extra properties are available and can only be used as functions:
Property
Description
if_
Only display the field if the function returns true.
value
Set the current value of the field.
Examples
Example 1: Dynamic fields based on form values
In this example we make a field required only if the user enters a value greater than 1000 in another field.
from typing import Unionfrom forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSinglefrom forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilderfrom forestadmin.datasource_toolkit.interfaces.actions import ActionResultasyncdefexecute(context: ActionContextSingle,result_builder: ResultBuilder) -> Union[None, ActionResult]:passagent.customize_collection("customer").add_action("Charge credit card", {"form": [ {"label": 'amount',"type": "Number", }, {"label": 'description',"type": "String",# The field will only be required if the function returns true."is_required": lambdacontext: context.form_values.get('amount', 0) >1000, }, ],"scope": "Single","execute": execute,})
Example 2: Conditional field display based on record data
Unlike the previous example, this one will only display the field if the record has a specific value.
It is still a dynamic field, but this time, the condition does not depend on the form values but on the record data.
from typing import Unionfrom forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSinglefrom forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilderfrom forestadmin.datasource_toolkit.interfaces.actions import ActionResultasyncdefshould_display_comment(context: ActionContextSingle): existing_comment =await context.get_record(['comment']).commentreturn existing_comment isNoneasyncdefexecute(context: ActionContextSingle,result_builder: ResultBuilder) -> Union[None, ActionResult]:# perform work hereagent.customize_collection("Product").add_action("Leave a review", {"form": [ {"label": "Rating","type": "Enum","enum_values": ["1", "2", "3", "4", "5"] }, {"label": "Put a comment","type": "String",# Only display this field the data a does not have comment already"if_": should_display_comment, }, ],})
Example 3: Conditional enum values based on both record data and form values
You can mix and match the previous examples to create complex forms.
In this example, the form will display a different set of enum values depending on both the record data and the value of the form field.
The first field displays different denominations that can be used to address the customer, depending on the full name and gender of the customer.
The second field displays different levels of loudness depending on if the customer is Morgan Freeman, as to ensure that we never speak Very Loudly at him, for the sake of politeness.
from typing import Unionfrom forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSinglefrom forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilderfrom forestadmin.datasource_toolkit.interfaces.actions import ActionResultdefget_loudly_enum_values(context: ActionContextSingle):# Enum values are computed based on the record data# Because we need to fetch the record data, we need to use an async function denomination = context.form_values.get("How should we refer to you?")if denomination =="Morgan Freeman":return ['Whispering','Softly','Loudly']else:return ['Softly','Loudly','Very Loudly']defget_denomination_enum_values(context: ActionContextSingle):# Enum values are computed based on another form field value# (no need to use an async function here, but doing so would not be a problem) person = context.get_record(['firstName', 'lastName', 'gender']) base = [person['firstName'],f"{person['firstName']}{person['lastName']}"]if (person['gender']=='Female'): base.append(f"Mrs. {person['lastName']}") base.append(f"Miss. {person['lastName']}")else: base.append(f"Mr. {person['lastName']}")return baseasyncdefexecute(context: ActionContextSingle,result_builder: ResultBuilder) -> Union[None, ActionResult]: denomination = context.form_values["How should we refer to you?"] loudness = context.form_values["How loud should we say it?"] text =f"Hello {denomination}"if loudness =='Whispering': text = text.lower()elif loudness =='Loudly': text = text.upper()elif loudness =='Very Loudly: text =f"{text.upper()}!!!"return result_builder.success(text)agent.customize_collection("customer").add_action("Tell me a greeting", {"form": [ {"label": "How should we refer to you?","type": "Enum","enum_values": get_denomination_enum_values } {"label": "How should we refer to you?","type": "Enum","enum_values": get_loudly_enum_values } ],"scope": "Single","execute": execute,})
Example 4: Using has_field_changed to reset value
In this example we reset a field based on change on another one.