Python Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
      • Flask
      • Django
    • Create your agent
    • Troubleshooting
    • Migrating legacy agents
      • Pre-requisites
      • Recommendations
      • Migration steps
      • Code transformations
        • API Charts
        • Live Queries
        • Smart Charts
        • Route overrides
        • Smart Actions
        • Smart Fields
        • Smart Relationships
        • Smart Segments
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • SQLAlchemy
      • Django
        • Polymorphic relationships
    • Write your own
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • In your code
  • In the admin panel

Was this helpful?

  1. Agent customization

Actions

PreviousGetting StartedNextScope and context

Last updated 4 months ago

Was this helpful?

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

Sooner or later, you will need to perform actions on your data that are specific to your business.

Moderating comments, generating invoices, logging into a customer’s account, or banning users are exactly the kind of important tasks to unlock to manage your day-to-day operations.

In your code

To create an Action, you will first need to declare it in your code for a specific collection. Here we declare a "Mark as Live" Action for the companies collection.

The action behavior is implemented in the execute function.

from typing import Union
from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> Union[None, ActionResult]:
    pass
    # implement your controller here

agent.customize_collection("companies").add_action("Mark as Live", {
    "scope": "Global",
    "execute": execute,  # this method can be a callable, awaitable or a lambda
    "description": "Mark the company as live",
    "submit_button_label": "Validate 🫵",
    "form": [
        {
            "type": "Layout",
            "component": 'Page',
            "next_button_label": "Go to address",
            "elements":[
                {"type": "Date", "label": "Live date","id": "LiveDate"},
                {"type": "Date", "label": "Exist since", "id": "CreationDate"},
                {"type": "Layout", "component": "Separator"},
                {
                    "type": "Layout",
                    "component": "Row",
                    "fields": [
                        {"type": "Boolean", "label": "CreditCard ?" "id": "WithCreditCard"},
                        {
                            "type": "String",
                            "if_": lambda ctx :ctx.form_values.get("WithCreditCard") is True,
                            "label": "Number",
                            "id": "CreditCardNumber",
                        },
                    ],
                },
            ],
        },
        {
            "type": "Layout",
            "component": "Page",
            "previous_button_label": "Go back to identity",
            "elements": [
                {
                    "type": "Layout",
                    "component": "Row",
                    "fields": [
                        {"type": "Number", "label": "StreetNumber"},
                        {"type": "String", "label": "StreetName"},
                    ],
                },
                {"type": "Layout", "component": "Separator"},
                {"type": "String", "label": "PostalCode"},
                {"type": "Number", "label": "City"},
                {"type": "Layout", "component": "Separator"},
                {"type": "String", "label": "Country"},
            ],
        },
    ],
})
Property
Usage
Description

scope

require

execute

require

form

optional

description

optional

An optional description of the action. Default: null

submit_button_label

optional

A custom label for the submit button. Default: the action name

In the admin panel

After declaring it, the Action will appear in the Smart Actions tab within your Collection Settings.

An Action is displayed in the UI only if:

  • it is set as "visible" (see screenshot below) AND

  • in non-development environments, the user's role must grant the "trigger" permission

You must make the Action visible there if you wish users to be able to see it in this Team.

Single, Global or Bulk. See for more detail

The callable called when the action is executed, with context and result builder as parameters. See and pages for more details.

A list of static fields to be input by the user or a function called with context as parameters which returns a list of fields. See page for more details.

here
form
result builder
context
Custom Action displayed in a Table View
Making the Action visible