[1.7.x] Fixed #23674 -- Fixed a crash when a MultiValueField has invalid data.
Backport of 0dea81cd6d34b3e41cc4bbec99b5fdf06142b09e from master
This commit is contained in:
parent
a189e9f0d0
commit
4b96e862b4
@ -1068,7 +1068,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`).
|
||||||
|
@ -1952,6 +1952,22 @@ class FormsTestCase(TestCase):
|
|||||||
self.assertFalse(id(field2.fields[0].choices) ==
|
self.assertFalse(id(field2.fields[0].choices) ==
|
||||||
id(field.fields[0].choices))
|
id(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…
x
Reference in New Issue
Block a user