Django

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

The Django data source allows importing collections from all models of your Django project.

To make everything work as expected, you need to install the package django.

Note that:

  • Django relationships will be respected

  • The Django data source works with multiple databases

  • The Django data source is added by default when using Django agent. You can disable this behavior by adding FOREST_AUTO_ADD_DJANGO_DATASOURCE = False in your settings.py

Customization function

from forestadmin.datasource_django.datasource import DjangoDatasource
from forestadmin.django_agent.agent import DjangoAgent

def customize_forest(agent: DjangoAgent):
    #this is done automatically when FOREST_AUTO_ADD_DJANGO_DATASOURCE=True (default)
    agent.add_datasource(DjangoDatasource())

Django and Async

The Forest Admin agent uses async internally, while Django ORM is part of Django async unsafe ↗. Under the hood, the agent uses sync_to_async to query the ORM.

For custom functions such as actions, computed fields, hooks, etc..., if an asynchronous function is specified, it will be called in an asynchronous thread (also known as an event loop). Whereas if it's a synchronous function, it will be executed directly within a synchronous thread. The method call will be wrapped with sync_to_async.

Because the agent core operates asynchronously, some APIs are asynchronous. This can result in a combination of asynchronous calls to the Forest Admin API and synchronous calls to Django ORM from custom functions.

Asynchronous function

In an asynchronous function, you can query the Django ORM with the async methods provided by Django ↗:

async def refund_order_execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> ActionResult:
    id_ = await context.get_record_id()
    order = await Order.objects.aget(id=id_)
    # ...
    return result_builder.success(f"{order.amount}$ refunded.")

Or use sync_to_async:

from asgiref.sync import sync_to_async

async def refund_order_execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> ActionResult:
    id_ = await context.get_record_id()
    order = await sync_to_async(Order.objects.get)(id=id_)
    # ...
    return result_builder.success(f"{order.amount}$ refunded.")

Synchronous function

In a synchronous context, you can query Forest Admin asynchronous methods with asyncio:

import asyncio

def refund_order_execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> ActionResult:
    id_ = asyncio.run(context.get_record_id())
    order = Order.objects.get(id=id_)
    # ...
    return result_builder.success(f"{order.amount}$ refunded.")

Or use async_to_sync:

from asgiref.sync import async_to_sync

def refund_order_execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> ActionResult:
    id_ = async_to_sync(context.get_record_id)()
    order = Order.objects.get(id=id_)
    # ...
    return result_builder.success(f"{order.amount}$ refunded.")

Last updated