From 90c8f8750247798669f84f7e32269e5c76317226 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Tue, 1 Jul 2025 18:01:20 +0300 Subject: [PATCH] Features: 1) Update URL generation logic in admin autocomplete to use `_meta` for app and model names; Fixes: 1) Ensure default value for `lookup_val` is an empty string if not provided; 2) Correct `field_name` parameter in the autocomplete URL to use `field_path`; Extra: 1) Minor cleanup by removing redundant variable declarations. --- core/admin.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/core/admin.py b/core/admin.py index c74cbd0c..92f57f51 100644 --- a/core/admin.py +++ b/core/admin.py @@ -49,15 +49,13 @@ class RelatedAutocompleteFilter(FieldListFilter): self.field_path = 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, "") - rel_model = field.remote_field.model - app_label = rel_model._meta.app_label - model_name = rel_model._meta.model_name - url_name = f"admin:{app_label}_{model_name}_autocomplete" + opts = model_admin.model._meta + url_name = f"admin:{opts.app_label}_{opts.model_name}_autocomplete" self.autocomplete_url = ( reverse(url_name) - + f"?field_name={field.name}&limit={self.limit}" + + f"?field_name={field_path}&limit={self.limit}" ) def expected_parameters(self): @@ -76,7 +74,7 @@ class RelatedAutocompleteFilter(FieldListFilter): "display": _("—"), "autocomplete_url": self.autocomplete_url, "lookup_kwarg": self.lookup_kwarg, - "lookup_val": self.lookup_val or "", + "lookup_val": self.lookup_val, }