From 913572ff56193eb4976746831bad7c1d2b74c859 Mon Sep 17 00:00:00 2001 From: Job Doesburg Date: Tue, 30 Jul 2024 15:18:34 +0200 Subject: [PATCH] Fix support for Django 5.0 --- pyproject.toml | 2 ++ src/django_admin_multi_select_filter/filters.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8fffb14..e87f72c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,8 @@ classifiers = [ "Framework :: Django :: 3.2", "Framework :: Django :: 4.0", "Framework :: Django :: 4.1", + "Framework :: Django :: 4.2", + "Framework :: Django :: 5.0", "Intended Audience :: Developers", "Operating System :: OS Independent", ] diff --git a/src/django_admin_multi_select_filter/filters.py b/src/django_admin_multi_select_filter/filters.py index c733afe..b6fce69 100644 --- a/src/django_admin_multi_select_filter/filters.py +++ b/src/django_admin_multi_select_filter/filters.py @@ -16,6 +16,9 @@ def __init__(self, field, request, params, model, model_admin, field_path): self.lookup_val = self.used_parameters.get(self.lookup_kwarg, []) if len(self.lookup_val) == 1 and self.lookup_val[0] == "": self.lookup_val = [] + elif len(self.lookup_val) == 1 and type(self.lookup_val[0]) != str: + # In Django 5.0, we get an extra list + self.lookup_val = self.lookup_val[0] self.lookup_val_isnull = self.used_parameters.get(self.lookup_kwarg_isnull) self.empty_value_display = model_admin.get_empty_value_display() @@ -30,9 +33,10 @@ def __init__(self, field, request, params, model, model_admin, field_path): ) self.field_verboses = {} if self.field.choices: - self.field_verboses = {field_value: field_verbose for - field_value, field_verbose in - self.field.choices} + self.field_verboses = { + field_value: field_verbose + for field_value, field_verbose in self.field.choices + } def expected_parameters(self): return [self.lookup_kwarg, self.lookup_kwarg_isnull] @@ -91,12 +95,16 @@ def __init__(self, field, request, params, model, model_admin, field_path): self.lookup_kwarg = "%s__%s__in" % (field_path, field.target_field.name) self.lookup_kwarg_isnull = "%s__isnull" % field_path values = params.get(self.lookup_kwarg, []) + if len(values) == 1 and type(values[0]) != str: + # In Django 5.0, we get an extra list + values = values[0] self.lookup_val = values.split(",") if values else [] self.lookup_choices = self.field_choices(field, request, model_admin) def choices(self, changelist): yield { - "selected": self.lookup_val is None and not self.lookup_val_isnull, + "selected": (self.lookup_val is None or self.lookup_val == []) + and not self.lookup_val_isnull, "query_string": changelist.get_query_string( remove=[self.lookup_kwarg, self.lookup_kwarg_isnull] ),