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.

from django.urls import reverse
from urllib.parse import urlencode

def get_admin_action_link(record, action):
app_label = record._meta.app_label
model = record._meta.model_name
id = record.id


changelist_url = reverse(f"admin:{app_label}_{model}_changelist")

query_params = {
    "action": action,
    "_selected_action": id,
}

base_url = f"{settings.env}.myurl.com" if settings.env else 
"http://localhost:8000"

full_url = f"{base_url}{changelist_url}?{urlencode(query_params)}"

return full_url
Back to Top