Features: 1) Add missing call to super().__init__() in admin field constructor;
Fixes: 1) Correct use of `remote` to `remote_opts` for consistent model metadata reference; Extra: 1) Improve formatting for better code readability.
This commit is contained in:
parent
83ac6b27e6
commit
5926ceb8bf
1 changed files with 12 additions and 9 deletions
|
|
@ -42,15 +42,17 @@ class RelatedAutocompleteFilter(FieldListFilter):
|
||||||
limit = 10
|
limit = 10
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
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_opts = field.remote_field.model._meta
|
||||||
base_url = reverse("admin:autocomplete")
|
base_url = reverse("admin:autocomplete")
|
||||||
self.autocomplete_url = (
|
self.autocomplete_url = (
|
||||||
f"{base_url}"
|
f"{base_url}"
|
||||||
f"?app_label={remote.app_label}"
|
f"?app_label={remote_opts.app_label}"
|
||||||
f"&model_name={remote.model_name}"
|
f"&model_name={remote_opts.model_name}"
|
||||||
f"&field_name={field.name}"
|
f"&field_name={field.name}"
|
||||||
f"&limit={self.limit}"
|
f"&limit={self.limit}"
|
||||||
)
|
)
|
||||||
|
|
@ -65,7 +67,8 @@ 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} if self.lookup_val else {}),
|
({self.lookup_kwarg: self.lookup_val}
|
||||||
|
if self.lookup_val else {}),
|
||||||
[]
|
[]
|
||||||
),
|
),
|
||||||
"display": _("—"),
|
"display": _("—"),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue