Skip to content

Commit

Permalink
Merge pull request #460 from foutrelis/fix-type-error-in-country-filter
Browse files Browse the repository at this point in the history
Fix TypeError in CountryFilter.choices()
  • Loading branch information
SmileyChris authored Apr 1, 2024
2 parents 0a50fcd + 7780268 commit 5d7600c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django_countries/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 17 additions & 4 deletions django_countries/tests/test_admin_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -51,13 +51,26 @@ 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))
self.assertEqual(
[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)

0 comments on commit 5d7600c

Please sign in to comment.