Features: 1) Update AutocompleteListFilter to dynamically display field label in placeholder using blocktrans; 2) Enhance choices method with field_label for improved context;
Fixes: 1) Remove redundant jQuery UI script and CSS imports for optimization; Extra: 1) Minor formatting adjustments for readability; 2) Replace variable `base_url` with `base` for naming consistency; 3) Improve `expected_parameters` method to return list format.
This commit is contained in:
parent
aff9a4cfa7
commit
08dc89ef19
2 changed files with 17 additions and 20 deletions
|
|
@ -43,14 +43,15 @@ 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.field_label = field.verbose_name
|
||||
|
||||
remote_opts = field.remote_field.model._meta
|
||||
base_url = reverse("admin:autocomplete")
|
||||
base = reverse("admin:autocomplete")
|
||||
self.autocomplete_url = (
|
||||
f"{base_url}"
|
||||
f"{base}"
|
||||
f"?app_label={remote_opts.app_label}"
|
||||
f"&model_name={remote_opts.model_name}"
|
||||
f"&field_name={field.name}"
|
||||
|
|
@ -58,7 +59,8 @@ class RelatedAutocompleteFilter(FieldListFilter):
|
|||
)
|
||||
|
||||
def expected_parameters(self):
|
||||
return f"{self.field_path}__{self.field.target_field.name}__exact"
|
||||
key = f"{self.field_path}__{self.field.target_field.name}__exact"
|
||||
return [key]
|
||||
|
||||
def has_output(self):
|
||||
return True
|
||||
|
|
@ -67,14 +69,13 @@ 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,
|
||||
"lookup_kwarg": self.lookup_kwarg,
|
||||
"lookup_val": self.lookup_val,
|
||||
"field_label": self.field_label,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,17 +7,13 @@
|
|||
|
||||
<label for="id_autocomplete_{{ spec.lookup_kwarg }}"></label><input type="text"
|
||||
id="id_autocomplete_{{ spec.lookup_kwarg }}"
|
||||
placeholder="{% trans 'Search…' %}"
|
||||
placeholder="{% blocktrans with label=spec.field_label %}Search by {{ label }}…{% endblocktrans %}"
|
||||
style="width:90%;"
|
||||
data-autocomplete-url="{{ spec.autocomplete_url }}"
|
||||
data-lookup-kwarg="{{ spec.lookup_kwarg }}"/>
|
||||
</div>
|
||||
|
||||
<script src="{% static 'admin/js/jquery.init.js' %}"></script>
|
||||
<script src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
|
||||
<script src="{% static 'admin/js/vendor/jquery/jquery.ui.js' %}"></script>
|
||||
<link rel="stylesheet"
|
||||
href="{% static 'admin/css/vendor/jquery-ui/jquery-ui.min.css' %}"/>
|
||||
<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}"></script>
|
||||
|
||||
<script>
|
||||
(function ($) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue