8000 fix: django 5's choice widget is not lazy by fsbraun · Pull Request #7707 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

fix: django 5's choice widget is not lazy #7707

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

Merged
merged 12 commits into from
Nov 30, 2023
Prev Previous commit
Next Next commit
Deprecate SuperLazyIterator and LazyChoiceField
  • Loading branch information
fsbraun committed Nov 29, 2023
commit 7fe90ea33d9ce818e5b8a75200170029a665f7b3
18 changes: 13 additions & 5 deletions cms/forms/fields.py
< 7D53 td id="diff-e85ff6f289e3d14e7361e3af4ee44957cafb551267e55071333e546dcef53a2cL23" data-line-number="23" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import warnings

from django import forms
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
from django.core.validators import EMPTY_VALUES
from django.forms import ChoiceField
from django.utils.translation import gettext_lazy as _

from cms.forms.utils import get_page_choices, get_site_choices
Expand All @@ -10,8 +13,10 @@
from cms.utils.compat import DJANGO_4_2


class SuperLazyIterator():
class SuperLazyIterator:
def __init__(self, func):
warnings.warn("SuperLazyIterator is deprecated.",
DeprecationWarning, stacklevel=2)
self.func = func

def __iter__(self):
Expand All @@ -20,6 +25,11 @@ def __iter__(self):

class LazyChoiceField(forms.ChoiceField):

def __init__(self, *args, **kwargs):
warnings.warn("LazyChoiceField is deprecated. Use Django's ChoiceField instead.",
DeprecationWarning, stacklevel=2)
super().__init__(*args, **kwargs)

@property
def choices(self):
return self._choices
Expand Down Expand Up @@ -54,13 +64,11 @@ def __init__(self, queryset=None, empty_label="---------", cache_choices=False,
errors = self.default_error_messages.copy()
if 'error_messages' in kwargs:
errors.update(kwargs['error_messages'])
site_choices = SuperLazyIterator(get_site_choices)
page_choices = SuperLazyIterator(get_page_choices)
self.limit_choices_to = limit_choices_to
kwargs['required'] = required
fields = (
LazyChoiceField(choices=site_choices, required=False, error_messages={'invalid': errors['invalid_site']}),
LazyChoiceField(choices=page_choices, required=False, error_messages={'invalid': errors['invalid_page']}),
ChoiceField(choices=get_site_choices, required=False, error_messages={'invalid': errors['invalid_site']}),
ChoiceField(choices=get_page_choices, required=False, error_messages={'invalid': errors['invalid_page']}),
)

# Remove the unexpected blank kwarg if it's supplied,
Expand Down
0