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 DjangoDatasourcefrom forestadmin.django_agent.agent import DjangoAgentdefcustomize_forest(agent: DjangoAgent):#this is done automatically when FOREST_AUTO_ADD_DJANGO_DATASOURCE=True (default) agent.add_datasource(DjangoDatasource())
FOREST_CUSTOMIZE_FUNCTION ="my_app.forest_admin.customize_agent"# or it can be a function directly# from my_app.forest_admin import customize_agent# FOREST_CUSTOMIZE_FUNCTION = my_app.forest_admin.customize_agent# FOREST_AUTO_ADD_DJANGO_DATASOURCE = True
Enable support of live queries
By enabling this feature, users with the required permission level can create Live Query components (charts, analytics charts and segments), allowing them to create more sophisticated requests to your database, by leveraging the underlying query language, SQL in this case.
You can enable this feature by setting a connection name (works as an identifier) when creating your datasource. This connection name will be reflected on the UI when configuring a Live Query component, it should have a clear meaning for your Forest users.
At this stage, ForestAdmin should display a connection field next to the live query input box.
Multi databases
If you are working with multiple databases and using the previous example, the connection main_database will be bind to the default database django is using.
To support multiple databases, the live_query_connection can support a mapping of {"connection_name": "database_name"}.
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.