From 2bf396c744fa27eae9ab55bed2af79f78a8e0e81 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Tue, 1 Jul 2025 19:56:40 +0300 Subject: [PATCH] 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. --- core/admin.py | 34 +++++++++---------- .../admin/autocomplete_list_filter.html | 20 +++++------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/core/admin.py b/core/admin.py index 148f1b40..d6564b16 100644 --- a/core/admin.py +++ b/core/admin.py @@ -44,12 +44,12 @@ class RelatedAutocompleteFilter(FieldListFilter): def __init__(self, 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_val = params.get(self.lookup_kwarg, "") + self.lookup_val = params.get(self.lookup_kwarg, "") remote = field.remote_field.model._meta - self.app_label = remote.app_label - self.model_name = remote.model_name - self.field_name = field.name + self.app_label = remote.app_label + self.model_name = remote.model_name + self.field_name = field.name self.field_label = field.verbose_name.lower() base = reverse("admin:autocomplete") @@ -79,21 +79,19 @@ class RelatedAutocompleteFilter(FieldListFilter): def choices(self, changelist): yield { - "selected": bool(self.lookup_val), - "query_string": changelist.get_query_string( - ({self.lookup_kwarg: self.lookup_val} - if self.lookup_val else {}), - [] - ), - "display": _("—"), + "selected": bool(self.lookup_val), + "query_string": changelist.get_query_string( + ({self.lookup_kwarg: self.lookup_val} if self.lookup_val else {}), [] + ), + "display": _("—"), "autocomplete_url": self.autocomplete_url, - "lookup_kwarg": self.lookup_kwarg, - "lookup_val": self.lookup_val, - "app_label": self.app_label, - "model_name": self.model_name, - "field_name": self.field_name, - "field_label": self.field_label, - "initial_text": self.initial_text, + "lookup_kwarg": self.lookup_kwarg, + "lookup_val": self.lookup_val, + "app_label": self.app_label, + "model_name": self.model_name, + "field_name": self.field_name, + "field_label": self.field_label, + "initial_text": self.initial_text, } diff --git a/core/templates/admin/autocomplete_list_filter.html b/core/templates/admin/autocomplete_list_filter.html index 4412789c..bcf8e602 100644 --- a/core/templates/admin/autocomplete_list_filter.html +++ b/core/templates/admin/autocomplete_list_filter.html @@ -18,21 +18,21 @@ {% endif %} + - - {% endblock %} {% block media %} {{ block.super }} - - + +