How to create a Django Admin link with preselected objects and a pre-filled action?

I am trying to generate a link that takes users to the Django Admin page for a specific model, with certain objects already selected and an action pre-filled in the dropdown menu.

Here’s what I’ve tried so far:

def get_admin_action_link(record, action):
    app_label = record._meta.app_label
    model = record._meta.model_name
    id = record.id
    env = f"{settings.env}.myurl.com" if settings.env else "http://localhost:8000"
    return f"{env}/admin/{app_label}/{model}?action={action}&_selected_action={id}"

The generated link looks like this:

http://localhost:8000/admin/app/mymodel?action=process&_selected_action=591

However, when I click on the link, it only takes me to the changelist view of the model in the admin. The objects aren’t selected, and the action isn’t pre-filled.

First, for generating Admin URLs, I’d use reverse():

reverse(f"admin:{app_label}_{model}_changelist")) + "?action=..."

I think that should be better if you change your overall Admin URL in the future.

But, as you’ve discovered, it doesn’t look like it’s possible to pre-set the action select field and checkboxes via a GET request. I tried to look through the Django code for what’s happening, but it’s a bit beyond me. It looks like something checks the URL parameters and redirects if it thinks they’re not valid (the redirect happens here).

Which is a shame, because you can do a similar thing with the add/change Admin views, pre-setting field values in a GET URL. Maybe there’s a good reason why it doesn’t work for the changelist.