Skip to content

Commit 6119d45

Browse files
authored
Added support for Django 5.0. (#1607)
1 parent e5fc05d commit 6119d45

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

django_filters/fields.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
RangeWidget,
1818
)
1919

20+
try:
21+
from django.utils.choices import BaseChoiceIterator, normalize_choices
22+
except ImportError:
23+
DJANGO_50 = False
24+
else:
25+
DJANGO_50 = True
26+
2027

2128
class RangeField(forms.MultiValueField):
2229
widget = RangeWidget
@@ -210,7 +217,7 @@ def clean(self, value):
210217
return value
211218

212219

213-
class ChoiceIterator:
220+
class ChoiceIterator(BaseChoiceIterator if DJANGO_50 else object):
214221
# Emulates the behavior of ModelChoiceIterator, but instead wraps
215222
# the field's _choices iterable.
216223

@@ -223,7 +230,10 @@ def __iter__(self):
223230
yield ("", self.field.empty_label)
224231
if self.field.null_label is not None:
225232
yield (self.field.null_value, self.field.null_label)
226-
yield from self.choices
233+
if DJANGO_50:
234+
yield from normalize_choices(self.choices)
235+
else:
236+
yield from self.choices
227237

228238
def __len__(self):
229239
add = 1 if self.field.empty_label is not None else 0
@@ -257,16 +267,21 @@ def __init__(self, *args, **kwargs):
257267

258268
super().__init__(*args, **kwargs)
259269

260-
def _get_choices(self):
261-
return super()._get_choices()
262-
263-
def _set_choices(self, value):
264-
super()._set_choices(value)
265-
value = self.iterator(self, self._choices)
266-
267-
self._choices = self.widget.choices = value
268-
269-
choices = property(_get_choices, _set_choices)
270+
@property
271+
def choices(self):
272+
return super().choices
273+
274+
@choices.setter
275+
def choices(self, value):
276+
if DJANGO_50:
277+
value = self.iterator(self, value)
278+
else:
279+
super()._set_choices(value)
280+
value = self.iterator(self, self._choices)
281+
282+
# Simple `super()` syntax for calling a parent property setter is
283+
# unsupported. See https://github.com/python/cpython/issues/59170
284+
super(ChoiceIteratorMixin, self.__class__).choices.__set__(self, value)
270285

271286

272287
# Unlike their Model* counterparts, forms.ChoiceField and forms.MultipleChoiceField do not set empty_label

tox.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,3 @@ commands = python -Werror ./runtests.py --testrunner xmlrunner.extra.djangotestr
4646
deps =
4747
{[latest]deps}
4848
-rrequirements/test-ci.txt
49-
50-
[testenv:{py310, py311}-latest]
51-
ignore_outcome = True

0 commit comments

Comments
 (0)