Opened 67 minutes ago

Last modified 34 minutes ago

#37176 assigned Bug

Action should be importable from django.contrib.admin.

Reported by: Mariusz Felisiak Owned by: Mariusz Felisiak
Component: contrib.admin Version: 6.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The new Action class should be importable from django.contrib.admin like ActionLocation.

Change History (2)

comment:1 by Natalia Bidart, 48 minutes ago

Triage Stage: UnreviewedAccepted

Thank you Mariusz, I think you proposal makes sense. I'm accepting but could you share your use case in the ticket for future reference?

in reply to:  1 comment:2 by Mariusz Felisiak, 34 minutes ago

Replying to Natalia Bidart:

Thank you Mariusz, I think you proposal makes sense. I'm accepting but could you share your use case in the ticket for future reference?

Sure, I'm adding actions dynamically based on the queryset, so separate action for each object, something like:

def get_actions(
        self,
        request,
        action_location=admin.ActionLocation.CHANGE_LIST,
    ):
        actions: dict[str, admin.Action] = super().get_actions(
            request,
            action_location=action_location, 
        )
        for some_obj in SomeModel.objects.all():
            name = f"add_{some_obj.pk}"
            description = f"Add {some_obj.name}"
            actions[name] = admin.Action(
                func=functools.partial(add_some_obj_action, some_obj=some_obj),
                name=name,
                description=description,
                plural_description=description,
                locations=[
                    admin.ActionLocation.CHANGE_LIST,
                    admin.ActionLocation.CHANGE_FORM,
                ],
            )
        return actions

I was surprised that I can use admin.ActionLocation but not admin.Action. The second thing is that our docs point to the django.contrib.admin module, that's why [source] is not visible for Action, check out https://docs.djangoproject.com/en/6.1/ref/contrib/admin/actions/#action.

Note: See TracTickets for help on using tickets.
Back to Top