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 { yield {
"selected": bool(self.lookup_val), "selected": bool(self.lookup_val),
"query_string": changelist.get_query_string( "query_string": changelist.get_query_string(
({self.lookup_kwarg: self.lookup_val} ({self.lookup_kwarg: self.lookup_val} if self.lookup_val else {}), []
if self.lookup_val else {}),
[]
), ),
"display": _(""), "display": _(""),
"autocomplete_url": self.autocomplete_url, "autocomplete_url": self.autocomplete_url,

View file

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