Features: 1) Enhance script initialization for Select2 in autocomplete_list_filter.html to improve compatibility.

Fixes: 1) Correct inconsistent spacing in `core/admin.py` field attribute assignments and methods.

Extra: 1) Format HTML and Python code for improved readability; 2) Replace inline script with `DOMContentLoaded` listener for better code structure.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-01 19:56:40 +03:00
parent 3c9c95d9bd
commit 2bf396c744
2 changed files with 26 additions and 28 deletions

View file

@ -81,9 +81,7 @@ class RelatedAutocompleteFilter(FieldListFilter):
yield {
"selected": bool(self.lookup_val),
"query_string": changelist.get_query_string(
({self.lookup_kwarg: self.lookup_val}
if self.lookup_val else {}),
[]
({self.lookup_kwarg: self.lookup_val} if self.lookup_val else {}), []
),
"display": _(""),
"autocomplete_url": self.autocomplete_url,

View file

@ -18,21 +18,21 @@
<option value="{{ spec.lookup_val }}" selected>{{ spec.initial_text }}</option>
{% endif %}
</select>
</div>
<script>
(function ($) {
$('#id_{{ spec.lookup_kwarg }}').djangoAdminSelect2();
})(django.jQuery);
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
const select = document.getElementById('id_{{ spec.lookup_kwarg }}');
if (typeof django !== 'undefined' && django.jQuery) {
django.jQuery(select).djangoAdminSelect2();
}
});
</script>
</div>
{% endblock %}
{% block media %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'admin/css/vendor/select2/select2.css' %}"/>
<link rel="stylesheet"
href="{% static 'admin/css/autocomplete.css' %}"/>
<link rel="stylesheet" href="{% static 'admin/css/vendor/select2/select2.css' %}"/>
<link rel="stylesheet" href="{% static 'admin/css/autocomplete.css' %}"/>
<script src="{% static 'admin/js/vendor/select2/select2.full.js' %}"></script>
<script src="{% static 'admin/js/autocomplete.js' %}"></script>