Bulk actions allow you to perform operations on multiple selected records. Define them using the @action decorator and list the method names in the actions attribute of your ModelAdmin.
Action Response Types
An action can return:
None: No special response.ActionResponseSchema with type=ActionResponseType.MESSAGE: Displays a message to the user.ActionResponseSchema with type=ActionResponseType.DOWNLOAD_BASE64: Triggers a file download (requires data as base64 content and file_name).
Running actions without selection
By default, the Apply button is disabled until rows are selected. To allow running an action on all objects without selecting any, set requires_selection=False. In this case, the ids argument will be an empty list.
Configuration options
actions: Tuple of action method names.actions_on_top: Boolean (default False) to show the actions bar at the top.actions_on_bottom: Boolean (default False) to show the actions bar at the bottom.actions_selection_counter: Boolean to show how many items are selected.
from fastadmin import TortoiseModelAdmin, action, register
from fastadmin import ActionResponseSchema, ActionResponseType
@register(Story)
class StoryAdmin(TortoiseModelAdmin):
actions = ("make_published",)
actions_on_top = False # actions bar at the top of the list page
actions_on_bottom = True # actions bar at the bottom
actions_selection_counter = True
@action(description="Mark selected stories as published")
async def make_published(self, objs: list) -> ActionResponseSchema | None:
for obj in objs:
obj.status = "published"
await obj.save()
return ActionResponseSchema(type=ActionResponseType.MESSAGE, data="Published!")
@action(description="Delete all stories", requires_selection=False)
async def delete_all(self, ids: list) -> None:
await Story.all().delete()