jowilf/starlette-admin

Enhancement: on_model_change feature

Closed this issue · 2 comments

agxc commented

Is your feature request related to a problem? Please describe.
I would like the ability to perform some actions before/after a model is created or updated.

I would like to have event features similar to what is flask-admin and sqladmin offers.
https://flask-admin.readthedocs.io/en/latest/_modules/flask_admin/model/base/#BaseModelView.on_model_change
OR
https://aminalaee.dev/sqladmin/api_reference/model_view/#sqladmin.models.ModelView.on_model_change

Describe the solution you'd like

For a column A in a model 1, I can:

parse column_a to a function func(column_a) which can be used to populate the values in column other columns

For example, parse_name(full_name) -> first_name & last_name. This is a simple example, but I can have a lot of other interesting implementations using this feature. The feature is available in other admin interfaces such as flask-admin flask-appbuilder or , sqladmin for FASTAPI.

Describe alternatives you've considered
I am deciding which framework to use. Starlette-admin looks very promising but may have to go with other ones for now.

jowilf commented

This feature is being added in #327. But, for the moment you can override the CRUDs methods.

Example:

class MyModelView(ModelView):
    async def create(self, request: Request, data: Dict) -> Any:
        # do something before
        obj = await super().create(request, data)
        # do something after
        return obj
agxc commented

Awesome stuff. Thank you for such prompt reply.