Skip to main content

Templates

The templates can be completely overridden and some of them are:
  • list_template: Template to use for list page. Default is fastdaisy_admin/list.html.
  • create_template: Template to use for creation page. Default is fastdaisy_admin/create.html.
  • edit_template: Template to use for edit page. Default is fastdaisy_admin/edit.html.
class UserAdmin(ModelView):
    model = User
    list_template = "custom_list.html"

Events

There are four methods that you can use to do some actions before or after a model was created, updated or deleted.
  • on_model_change: Called before a model was created or updated.
  • after_model_change: Called after a model was created or updated.
  • on_model_delete: Called before a model was deleted.
  • after_model_delete: Called after a model was deleted.

class UserAdmin(ModelView):
    model = User
    async def on_model_change(self, data, model, is_created, request):
        # Perform some other action
        ...

    async def on_model_delete(self, model, request):
        # Perform some other action
        ...

Custom Action

The available options for an action include:
  • name: If not explicitly provided, the function name will be used as the action name which is displayed in the action dropdown on the list page.
Although there is a built-in export feature that allows users to export records in CSV or JSON format, the example below demonstrates how to create a custom action to export selected records.

import json
from fastdaisy_admin import BaseView, action



@action(
        name="Export Json"
    )
async def export_to_json(modelview, request, objs):
    data = [
        {"id": u.id, "name": u.name, "email": u.email, "is_active": u.is_admin}
        for u in objs
    ]
    return Response(
        content=json.dumps(data, indent=2),
        media_type="application/json",
        headers={"Content-Disposition": "attachment; filename=users_export.json"}
    )

class UserAdmin(ModelView):
    model = User
    actions = [export_to_json]
    
admin.add_view(UserAdmin)

In the above example, we created export_to_json function, It can be define anywhere which should contain three argument.
  • modelview: Instance of ModelView
  • request: It’s a HTTP request
  • objs: Selected objects of respective modelview