This is the official documentation of the forestadmin-agent-django
and forestadmin-agent-flask
Python agents.
Forest Admin allows replacing the default field writing behavior with your own custom logic.
This is useful when you want to change how a given field behaves, but also to make writable.
How does it work
The replace_field_writing
function allows to change the behavior of any change by creating a new patch that will be applied to the record.
You should refrain from using handlers that have side effects (to perform error handling, validation, ...) and .
Making a field read-only
Examples
Changing other fields in the same record
In the following example, editing or creating a fullName
will update both firstName
and lastName
fields of the record.
Copy from forestadmin.datasource_toolkit.decorators.write.write_replace.write_customization_context import (
WriteCustomizationContext,
)
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias
def full_name_write_fn(
value, context: WriteCustomizationContext
) -> RecordsDataAlias:
first_name, last_name = value.split(' ')
return {"firstName": first_name, "LastName": last_name}
collection.replace_field_writing('fullName', full_name_write_fn)
Having specific behavior only for updates
You can have different behavior for creations
and updates
.
In this example, each time the firstName
field is edited, we also want to update a timestamp field.
Copy from datetime import date
from forestadmin.datasource_toolkit.decorators.write.write_replace.write_customization_context import (
WriteCustomizationContext,
)
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias
def first_name_write_fn(
value, context: WriteCustomizationContext
) -> RecordsDataAlias:
if context.action == "create":
return {"firstName": value, "firstNameLastEdited": None}
elif context.action == "update":
return {"firstName": value, "firstNameLastEdited": date.today().isoformat()}
else:
raise Exception("Unexpected value")
collection.replace_field_writing('firstName', first_name_write_fn)
In this simple example, we have two collections that are linked together:
The Users
collection has a job
and a portfolioId
as foreignKey
The Portfolios
collection has a title
When the user updates his job
field we want also to update the title
of the portfolio by the job
name.
Copy collection.replace_field_writing(
"job", lambda value, context: {"job": value, "portfolio": {"title": value}}
)
You can also provide another portfolioId
to update the relationships and their fields:
Copy collection.replace_field_writing(
"job",
lambda value, context: {
"job": value,
"portfolioId": 8,
"portfolio": {"title": value},
},
)
Of course, you can chain the relationships. For example, if a portfolio has a one-to-one
relationship with the formats
collection, you can update it by writing the right path.
Copy collection.replace_field_writing(
"job",
lambda value, context: {
"job": value,
"portfolioId": 8,
"portfolio": {"title": value, "format": {"name": "pdf"}},
},
)