Last active
January 6, 2023 13:33
-
-
Save cb109/c39f1a6b107a05b233bb463e590462b0 to your computer and use it in GitHub Desktop.
Turn Django admin autocomplete_field labels into links to the chosen model instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ = django.jQuery; | |
for (const formRow of $('.form-row')) { | |
const label = formRow.getElementsByTagName('label')[0]; | |
const relatedWidgetWrapper = formRow.querySelector('.related-widget-wrapper'); | |
if (!label || !relatedWidgetWrapper) { | |
continue; | |
} | |
const anchor = relatedWidgetWrapper.querySelector('a'); | |
const urlWithoutParams = anchor.href.split('?')[0]; | |
if (!!urlWithoutParams) { | |
const labelText = label.innerText; | |
label.innerHTML = '<a href="' + urlWithoutParams + '" target="_blank">' + labelText + '</a>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns this:
Into this:
Tested with Django 3.2.