Skip to content

Commit

Permalink
Added support for Django 5.0. (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope authored Sep 4, 2023
1 parent e5fc05d commit 6119d45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
39 changes: 27 additions & 12 deletions django_filters/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
RangeWidget,
)

try:
from django.utils.choices import BaseChoiceIterator, normalize_choices
except ImportError:
DJANGO_50 = False
else:
DJANGO_50 = True


class RangeField(forms.MultiValueField):
widget = RangeWidget
Expand Down Expand Up @@ -210,7 +217,7 @@ def clean(self, value):
return value


class ChoiceIterator:
class ChoiceIterator(BaseChoiceIterator if DJANGO_50 else object):
# Emulates the behavior of ModelChoiceIterator, but instead wraps
# the field's _choices iterable.

Expand All @@ -223,7 +230,10 @@ def __iter__(self):
yield ("", self.field.empty_label)
if self.field.null_label is not None:
yield (self.field.null_value, self.field.null_label)
yield from self.choices
if DJANGO_50:
yield from normalize_choices(self.choices)
else:
yield from self.choices

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

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

def _get_choices(self):
return super()._get_choices()

def _set_choices(self, value):
super()._set_choices(value)
value = self.iterator(self, self._choices)

self._choices = self.widget.choices = value

choices = property(_get_choices, _set_choices)
@property
def choices(self):
return super().choices

@choices.setter
def choices(self, value):
if DJANGO_50:
value = self.iterator(self, value)
else:
super()._set_choices(value)
value = self.iterator(self, self._choices)

# Simple `super()` syntax for calling a parent property setter is
# unsupported. See https://github.com/python/cpython/issues/59170
super(ChoiceIteratorMixin, self.__class__).choices.__set__(self, value)


# Unlike their Model* counterparts, forms.ChoiceField and forms.MultipleChoiceField do not set empty_label
Expand Down
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ commands = python -Werror ./runtests.py --testrunner xmlrunner.extra.djangotestr
deps =
{[latest]deps}
-rrequirements/test-ci.txt

[testenv:{py310, py311}-latest]
ignore_outcome = True

0 comments on commit 6119d45

Please sign in to comment.