Fixed #23674 -- Fixed a crash when a MultiValueField has invalid data.
This commit is contained in:
parent
43041ee48c
commit
0dea81cd6d
|
@ -1104,7 +1104,11 @@ class MultiValueField(Field):
|
||||||
if not isinstance(initial, list):
|
if not isinstance(initial, list):
|
||||||
initial = self.widget.decompress(initial)
|
initial = self.widget.decompress(initial)
|
||||||
for field, initial, data in zip(self.fields, initial, data):
|
for field, initial, data in zip(self.fields, initial, data):
|
||||||
if field.has_changed(field.to_python(initial), data):
|
try:
|
||||||
|
initial = field.to_python(initial)
|
||||||
|
except ValidationError:
|
||||||
|
return True
|
||||||
|
if field.has_changed(initial, data):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -119,3 +119,5 @@ Bugfixes
|
||||||
* Fixed bug in ``makemigrations`` that created broken migration files when
|
* Fixed bug in ``makemigrations`` that created broken migration files when
|
||||||
dealing with multiple table inheritance and inheriting from more than one
|
dealing with multiple table inheritance and inheriting from more than one
|
||||||
model (:ticket:`23956`).
|
model (:ticket:`23956`).
|
||||||
|
|
||||||
|
* Fixed a crash when a ``MultiValueField`` has invalid data (:ticket:`23674`).
|
||||||
|
|
|
@ -2006,6 +2006,22 @@ class FormsTestCase(TestCase):
|
||||||
self.assertIsNot(field2.fields, field.fields)
|
self.assertIsNot(field2.fields, field.fields)
|
||||||
self.assertIsNot(field2.fields[0].choices, field.fields[0].choices)
|
self.assertIsNot(field2.fields[0].choices, field.fields[0].choices)
|
||||||
|
|
||||||
|
def test_multivalue_initial_data(self):
|
||||||
|
"""
|
||||||
|
#23674 -- invalid initial data should not break form.changed_data()
|
||||||
|
"""
|
||||||
|
class DateAgeField(MultiValueField):
|
||||||
|
def __init__(self, fields=(), *args, **kwargs):
|
||||||
|
fields = (DateField(label="Date"), IntegerField(label="Age"))
|
||||||
|
super(DateAgeField, self).__init__(fields=fields, *args, **kwargs)
|
||||||
|
|
||||||
|
class DateAgeForm(Form):
|
||||||
|
date_age = DateAgeField()
|
||||||
|
|
||||||
|
data = {"date_age": ["1998-12-06", 16]}
|
||||||
|
form = DateAgeForm(data, initial={"date_age": ["200-10-10", 14]})
|
||||||
|
self.assertTrue(form.has_changed())
|
||||||
|
|
||||||
def test_multivalue_optional_subfields(self):
|
def test_multivalue_optional_subfields(self):
|
||||||
class PhoneField(MultiValueField):
|
class PhoneField(MultiValueField):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
Loading…
Reference in New Issue