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
  • Displaying a link to the last message sent by a customer
  • Connecting collections without having a shared identifier

Was this helpful?

  1. Agent customization
  2. Relationships

Computed foreign keys

PreviousTo multiple recordsNextUnder the hood

Last updated 1 year ago

Was this helpful?

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

You may want to create a relationship between 2 Collections, but you don't have a foreign key that is ready to use to connect them.

To solve that use case, you should use both and relationships.

This is done with the following steps:

  • create a new Field containing a foreign key,

  • make the Field filterable for the In operator (see as to why this is required),

  • create a relation using it.

Displaying a link to the last message sent by a customer

We have 2 Collections: Customers and Messages, linked together by a one-to-many relationship.

We want to create a ManyToOne relationship with the last message sent by a given customer.

from forestadmin.datasource_toolkit.context.collection_context import (
    CollectionCustomizationContext,
)
from forestadmin.datasource_toolkit.interfaces.fields import Operator
from forestadmin.datasource_toolkit.interfaces.query.aggregation import (
    Aggregation,
    Aggregator,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.base import (
    ConditionTree,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import (
    ConditionTreeLeaf,
)
from forestadmin.datasource_toolkit.interfaces.query.filter.unpaginated import Filter
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias

async def get_last_message_id(
    customers: RecordsDataAlias, context: CollectionCustomizationContext
):
    # We're using Forest Admin's Query Interface (you can use an ORM or plain SQL)
    messages = context.datasource.get_collection("messages")
    condition_tree = ConditionTreeLeaf(
        "customer_id", Operator.IN, [c["id"] for c in customers]
    )

    rows = await messages.aggregate(
        context.caller,
        Filter({"condition_tree": condition_tree}),
        Aggregation(
            {
                "field": "id",
                "operation": "Max",
                "groups": [{"field": "customer_id"}],
            }
        ),
    )
    ret = []
    for customer in customers:
        row = filter(lambda r: r["group"]["customer_id"] == customer["id"], rows)
        row = row[0] if len(row) == 1 else {}
        ret.append(row.get("value"))
    return ret

async def last_message_in_operator(
    last_message_ids: List[Any], context: CollectionCustomizationContext
) -> ConditionTree:
    records = await context.datasource.get_collection("messages").list(
        context.caller,
        Filter(
            {
                "condition_tree": ConditionTreeLeaf(
                    "id", Operator.IN, last_message_ids
                )
            }
        ),
        ["customer_id"],
    )
    return ConditionTreeLeaf("id", Operator.IN, [r["customer_id"] for r in records])

agent.customize_collection("Customers").add_field(
    "lastMessageId",
    {
        # Create foreign key
        "column_type": "Number",
        "dependencies": ["id"],
        "get_values": get_last_message_id,
    },
).replace_field_operator(
    # Implement the 'In' operator.
    "lastMessageId",
    Operator.IN,
    last_message_in_operator,
).add_many_to_one_relation(
    # Create relationships using the foreign key we just added.
    name="lastMessage",
    foreign_collection="messages",
    foreign_key="lastMessageId",
)

Connecting collections without having a shared identifier

You have 2 Collections and both contain users: one comes from your database, and the other one is connected to the CRM that your company uses.

There is no common id between them that can be used to tell Forest Admin how to link them together, however, both Collections have firstName, lastName, and birthDate fields, which taken together, are unique enough.

from forestadmin.datasource_toolkit.datasource_customizer.collection_customizer import (
    CollectionCustomizer,
)
from forestadmin.datasource_toolkit.interfaces.fields import Operator
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.branch import (
    Aggregator,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.branch import (
    ConditionTreeBranch,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import (
    ConditionTreeLeaf,
)

# Concatenate firstname, lastname and birthData to make a unique identifier
# and ensure that the new field is filterable
def create_filterable_identity_field(collection: CollectionCustomizer):
    # Create foreign key on the collection from the database
    collection.add_field(
        "userIdentifier",
        {
            "column_type": "String",
            "dependencies": ["firstName", "lastName", "birthDate"],
            "get_values": lambda users, context: [
                f"{u['firstName']}/{u['lastName']}/{u['birthDate']}" for u in users
            ],
        },
    )

    # Implement 'In' filtering operator (required)
    collection.replace_field_operator(
        "userIdentifier",
        Operator.IN,
        lambda values, ctx: ConditionTreeBranch(
            Aggregator.OR,
            [
                ConditionTreeBranch(
                    Aggregator.AND,
                    [
                        ConditionTreeLeaf(
                            "firstName", Operator.EQUAL, value.split("/")[0]
                        ),
                        ConditionTreeLeaf(
                            "lastName", Operator.EQUAL, value.split("/")[1]
                        ),
                        ConditionTreeLeaf(
                            "birthDate", Operator.EQUAL, value.split("/")[2]
                        ),
                    ],
                )
                for value in values
            ],
        ),
    )

create_filterable_identity_field(agent.customize_collection("databaseUsers"))
create_filterable_identity_field(agent.customize_collection("crmUsers"))

# Create relationship between databaseUsers and crmUsers
agent.customize_collection("databaseUsers").add_one_to_one_relation(
    name="userFromCrm",
    foreign_collection="crmUsers",
    origin_key="userIdentifier",
    origin_key_target="userIdentifier",
)
# Create relationship between crmUsers and databaseUsers
agent.customize_collection("databaseUsers").add_many_to_one_relation(
    name="userFromCrm",
    foreign_collection="crmUsers",
    foreign_key="userIdentifier",
    foreign_key_target="userIdentifier",
)
computed fields
Under the hood