From bae099f8008c47519a1653b620d44155bfe532bb Mon Sep 17 00:00:00 2001 From: Evangelos Foutras Date: Mon, 1 Apr 2024 14:10:17 +0300 Subject: [PATCH 1/2] Fix TypeError in CountryFilter.choices() When no country is selected, self.used_parameters.get(self.field.name) will return None. This raises a TypeError when CountryFilter.choices() tries to check whether a specific country choice is currently selected. Avoid this by checking that the parameter is not empty similarly to how Django's built-in admin.filters.RelatedFieldListFilter checks for this. Fixes: 04662d1e282d ("Support Django 5.0") --- django_countries/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_countries/filters.py b/django_countries/filters.py index beb653ce..eb5a150e 100644 --- a/django_countries/filters.py +++ b/django_countries/filters.py @@ -24,7 +24,7 @@ def choices(self, changelist): } for lookup, title in self.lookup_choices(changelist): if django.VERSION >= (5, 0): - selected = force_str(lookup) in value + selected = value is not None and force_str(lookup) in value else: selected = force_str(lookup) == value yield { From 77802684e01d386e3d90d506e1ff5fa208a6f10f Mon Sep 17 00:00:00 2001 From: Evangelos Foutras Date: Mon, 1 Apr 2024 17:45:01 +0300 Subject: [PATCH 2/2] Test CountryFilter.choices() with empty selection Add a regression test for the case where no country has been specified in the filter parameters. There was already a test where a country was given, but the issue in question only occurred when none was selected. --- django_countries/tests/test_admin_filters.py | 21 ++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/django_countries/tests/test_admin_filters.py b/django_countries/tests/test_admin_filters.py index 25c8e642..ce66d0f0 100644 --- a/django_countries/tests/test_admin_filters.py +++ b/django_countries/tests/test_admin_filters.py @@ -6,7 +6,7 @@ from django.test import TestCase from django.test.client import RequestFactory -from django_countries import filters +from django_countries import countries, filters from django_countries.tests import models test_site = admin.AdminSite(name="test-admin") @@ -51,8 +51,15 @@ def test_filter_country(self): list(cl.result_list), list(models.Person.objects.exclude(country="AU")) ) - def test_choices(self): - request = RequestFactory().get("/person/", data={"country": "NZ"}) + def _test_choices(self, selected_country_code="NZ"): + request_params = {} + selected_country = "All" + + if selected_country_code: + request_params["country"] = selected_country_code + selected_country = countries.name(selected_country_code) + + request = RequestFactory().get("/person/", data=request_params) request.user = AnonymousUser() cl = ChangeList(request, **self.get_changelist_kwargs()) choices = list(cl.filter_specs[0].choices(cl)) @@ -60,4 +67,10 @@ def test_choices(self): [c["display"] for c in choices], ["All", "Australia", "New Zealand"] ) for choice in choices: - self.assertEqual(choice["selected"], choice["display"] == "New Zealand") + self.assertEqual(choice["selected"], choice["display"] == selected_country) + + def test_choices(self): + return self._test_choices() + + def test_choices_empty_selection(self): + return self._test_choices(selected_country_code=None)