17
17
RangeWidget ,
18
18
)
19
19
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
+
20
27
21
28
class RangeField (forms .MultiValueField ):
22
29
widget = RangeWidget
@@ -210,7 +217,7 @@ def clean(self, value):
210
217
return value
211
218
212
219
213
- class ChoiceIterator :
220
+ class ChoiceIterator ( BaseChoiceIterator if DJANGO_50 else object ) :
214
221
# Emulates the behavior of ModelChoiceIterator, but instead wraps
215
222
# the field's _choices iterable.
216
223
@@ -223,7 +230,10 @@ def __iter__(self):
223
230
yield ("" , self .field .empty_label )
224
231
if self .field .null_label is not None :
225
232
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
227
237
228
238
def __len__ (self ):
229
239
add = 1 if self .field .empty_label is not None else 0
@@ -257,16 +267,21 @@ def __init__(self, *args, **kwargs):
257
267
258
268
super ().__init__ (* args , ** kwargs )
259
269
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 )
270
285
271
286
272
287
# Unlike their Model* counterparts, forms.ChoiceField and forms.MultipleChoiceField do not set empty_label
0 commit comments