Override filtering behavior

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

You may want to read about the following topics before using those features:

Disabling operators

Disabling filtering can be made without any code in the field settings ↗.

Substitution

Operation substitution can be used for two motives:

  • Performance: provide a more efficient way to perform a given filtering operation

  • Capabilities: enable filtering on a computed field or other non-filterable fields

from forestadmin.datasource_toolkit.context.collection_context import CollectionCustomizationContext
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.base import ConditionTree
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.branch import ConditionTreeBranch
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import ConditionTreeLeaf

async def full_name_equal_fn(
    value, context: CollectionCustomizationContext
) -> ConditionTree:
    first_name, last_names = value.split(" ", 1)
    return ConditionTreeBranch(
        "and",
        [
            ConditionTreeLeaf("firstName", "equal", first_name),
            ConditionTreeLeaf("lastName", "equal", last_names),
        ],
    )

collection.replace_field_operator("fullName", "equal", full_name_equal_fn)

You can use the replaceFieldOperator method to unlock the operators.

Emulation

Filtering emulation allows making fields filterable automatically. It is a convenient way to get things working quickly for Collections that have a low number of records (in the thousands at most).

This emulation forces the Agent to retrieve all the Collection records and compute the field values for each one of them. As a consequence, filtering emulation performance cost is linear with the number of records in the Collection, so activate it sparingly and with great care.

# Add support for all operators
collection.emulate_field_filtering("fullName")

# Add support for a single operator
collection.emulate_field_filtering("fullName", "equal")

Last updated