-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes for Django 5.0. #1606
Fixes for Django 5.0. #1606
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,14 @@ | |
RangeWidget, | ||
) | ||
|
||
# Compat for Django >= 5.0 | ||
try: | ||
from django.utils.choices import normalize_choices | ||
except ImportError: | ||
normalize_choices = None | ||
|
||
DJANGO_50 = normalize_choices is not None | ||
|
||
|
||
class RangeField(forms.MultiValueField): | ||
widget = RangeWidget | ||
|
@@ -257,22 +265,32 @@ def __init__(self, *args, **kwargs): | |
|
||
super().__init__(*args, **kwargs) | ||
|
||
def _get_choices(self): | ||
return super()._get_choices() | ||
if not DJANGO_50: | ||
|
||
def _get_choices(self): | ||
return super()._get_choices() | ||
|
||
def _set_choices(self, value): | ||
super()._set_choices(value) | ||
value = self.iterator(self, self._choices) | ||
def _set_choices(self, value): | ||
super()._set_choices(value) | ||
value = self.iterator(self, self._choices) | ||
|
||
self._choices = self.widget.choices = value | ||
self._choices = self.widget.choices = value | ||
|
||
choices = property(_get_choices, _set_choices) | ||
choices = property(_get_choices, _set_choices) | ||
|
||
|
||
# Unlike their Model* counterparts, forms.ChoiceField and forms.MultipleChoiceField do not set empty_label | ||
class ChoiceField(ChoiceIteratorMixin, forms.ChoiceField): | ||
iterator = ChoiceIterator | ||
|
||
if DJANGO_50: | ||
|
||
@forms.ChoiceField.choices.setter | ||
def choices(self, value): | ||
self._choices = self.widget.choices = self.iterator( | ||
self, normalize_choices(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few interesting things here:
I think all of this can be solved. Will open an alternate PR soon... |
||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.empty_label = kwargs.pop("empty_label", settings.EMPTY_CHOICE_LABEL) | ||
super().__init__(*args, **kwargs) | ||
|
@@ -281,6 +299,14 @@ def __init__(self, *args, **kwargs): | |
class MultipleChoiceField(ChoiceIteratorMixin, forms.MultipleChoiceField): | ||
iterator = ChoiceIterator | ||
|
||
if DJANGO_50: | ||
|
||
@forms.MultipleChoiceField.choices.setter | ||
def choices(self, value): | ||
self._choices = self.widget.choices = self.iterator( | ||
self, normalize_choices(value) | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.empty_label = None | ||
super().__init__(*args, **kwargs) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ | |
import os | ||
import sys | ||
|
||
import django | ||
from django.core.management import execute_from_command_line | ||
|
||
|
||
def runtests(): | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings") | ||
django.setup() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this was due to a deep circular import issue that didn't flag up when running Django's test suite. See django/django#17215 for where this was fixed. |
||
argv = sys.argv[:1] + ["test"] + sys.argv[1:] | ||
execute_from_command_line(argv) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be simplified: