AdminActionBaseClass¶
- class action_hero.lib.AdminActionBaseClass¶
Bases:
ABCGenerates an admin action that calls a function for a chosen set of records.
Yes, it’s basically an abstracted
map.Example usage:
class MyAdminAction(AdminActionBaseClass): def handle_item(self, item): self.function(item) print(f"{item.pk} has been handled.") # Example 1: Using only name (auto-generated short_description) conditional_action = MyAdminAction( function=my_function, condition=lambda record: record.should_process(), name="process_records", ) # Example 2: Explicitly overriding short_description (recommended Django-style) custom_label_action = MyAdminAction( function=my_function, name="bulk_process", short_description="Process Selected Records", ) def my_function(record): # perform work on a single model instance class MyModelAdmin(admin.ModelAdmin): actions = [conditional_action, custom_label_action] model = MyModel
If you need custom behavior, subclass
AdminActionBaseClassand override the appropriate method(s). See implementations inactionsfor details.- __call__(modeladmin, request, queryset)¶
Calls
self.handle_itemfor each item inquerysetthat passesself.condition.- Return type:
None- Parameters:
modeladmin (ModelAdmin) – The admin instance for the model being processed.
request (HttpRequest) – The current HTTP request object.
queryset (QuerySet) – The queryset of records to process.
- __init__(function, *, condition=None, name=None, short_description=None)¶
Initializes the action with a function and an optional condition.
- Parameters:
function (Function) – Callable that takes a model instance.
condition (Condition | None) – Callable for whether to process each record.
name (str | None) – Internal identifier used by Django admin if
short_descriptionis not provided.short_description (str | None) – User-facing label shown in the Django admin dropdown.
- Return type:
None
- abstractmethod handle_item(item)¶
Handles a single item from the queryset.
This method will be called for each item in the queryset that passes the condition. Any subclass must implement this method to define how to process each item.
Note
This method is not asynchronous or in another thread/process; large quantities of work should be done in other ways or places, not solely in this method.
- Parameters:
item (Model) – The model instance being processed.