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:
parent
3c9c95d9bd
commit
2bf396c744
2 changed files with 26 additions and 28 deletions
|
|
@ -44,12 +44,12 @@ class RelatedAutocompleteFilter(FieldListFilter):
|
||||||
def __init__(self, field, request, params, model, model_admin, field_path):
|
def __init__(self, field, request, params, model, model_admin, field_path):
|
||||||
super().__init__(field, request, params, model, model_admin, field_path)
|
super().__init__(field, request, params, model, model_admin, field_path)
|
||||||
self.lookup_kwarg = f"{field_path}__{field.target_field.name}__exact"
|
self.lookup_kwarg = f"{field_path}__{field.target_field.name}__exact"
|
||||||
self.lookup_val = params.get(self.lookup_kwarg, "")
|
self.lookup_val = params.get(self.lookup_kwarg, "")
|
||||||
|
|
||||||
remote = field.remote_field.model._meta
|
remote = field.remote_field.model._meta
|
||||||
self.app_label = remote.app_label
|
self.app_label = remote.app_label
|
||||||
self.model_name = remote.model_name
|
self.model_name = remote.model_name
|
||||||
self.field_name = field.name
|
self.field_name = field.name
|
||||||
self.field_label = field.verbose_name.lower()
|
self.field_label = field.verbose_name.lower()
|
||||||
|
|
||||||
base = reverse("admin:autocomplete")
|
base = reverse("admin:autocomplete")
|
||||||
|
|
@ -79,21 +79,19 @@ class RelatedAutocompleteFilter(FieldListFilter):
|
||||||
|
|
||||||
def choices(self, changelist):
|
def choices(self, changelist):
|
||||||
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,
|
||||||
"lookup_kwarg": self.lookup_kwarg,
|
"lookup_kwarg": self.lookup_kwarg,
|
||||||
"lookup_val": self.lookup_val,
|
"lookup_val": self.lookup_val,
|
||||||
"app_label": self.app_label,
|
"app_label": self.app_label,
|
||||||
"model_name": self.model_name,
|
"model_name": self.model_name,
|
||||||
"field_name": self.field_name,
|
"field_name": self.field_name,
|
||||||
"field_label": self.field_label,
|
"field_label": self.field_label,
|
||||||
"initial_text": self.initial_text,
|
"initial_text": self.initial_text,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
<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>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
(function ($) {
|
|
||||||
$('#id_{{ spec.lookup_kwarg }}').djangoAdminSelect2();
|
|
||||||
})(django.jQuery);
|
|
||||||
</script>
|
|
||||||
{% 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>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue