Fixed #22539 -- Copied exclude argument in Model.full_clean() to prevent side effects.
This commit is contained in:
parent
45c2d1f5d9
commit
e2e4cdba11
|
@ -988,6 +988,8 @@ class Model(six.with_metaclass(ModelBase)):
|
||||||
errors = {}
|
errors = {}
|
||||||
if exclude is None:
|
if exclude is None:
|
||||||
exclude = []
|
exclude = []
|
||||||
|
else:
|
||||||
|
exclude = list(exclude)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.clean_fields(exclude=exclude)
|
self.clean_fields(exclude=exclude)
|
||||||
|
|
|
@ -3,9 +3,9 @@ from django.test import TestCase
|
||||||
|
|
||||||
|
|
||||||
class ValidationTestCase(TestCase):
|
class ValidationTestCase(TestCase):
|
||||||
def assertFailsValidation(self, clean, failed_fields):
|
def assertFailsValidation(self, clean, failed_fields, **kwargs):
|
||||||
with self.assertRaises(ValidationError) as cm:
|
with self.assertRaises(ValidationError) as cm:
|
||||||
clean()
|
clean(**kwargs)
|
||||||
self.assertEqual(sorted(failed_fields), sorted(cm.exception.message_dict))
|
self.assertEqual(sorted(failed_fields), sorted(cm.exception.message_dict))
|
||||||
|
|
||||||
def assertFieldFailsValidationWithMessage(self, clean, field_name, message):
|
def assertFieldFailsValidationWithMessage(self, clean, field_name, message):
|
||||||
|
|
|
@ -59,6 +59,13 @@ class BaseModelValidationTests(ValidationTestCase):
|
||||||
mtv = ModelToValidate(number=10, name='Some Name', slug='##invalid##')
|
mtv = ModelToValidate(number=10, name='Some Name', slug='##invalid##')
|
||||||
self.assertFailsValidation(mtv.full_clean, ['slug'])
|
self.assertFailsValidation(mtv.full_clean, ['slug'])
|
||||||
|
|
||||||
|
def test_full_clean_does_not_mutate_exclude(self):
|
||||||
|
mtv = ModelToValidate(f_with_custom_validator=42)
|
||||||
|
exclude = ['number']
|
||||||
|
self.assertFailsValidation(mtv.full_clean, ['name'], exclude=exclude)
|
||||||
|
self.assertEqual(len(exclude), 1)
|
||||||
|
self.assertEqual(exclude[0], 'number')
|
||||||
|
|
||||||
|
|
||||||
class ArticleForm(forms.ModelForm):
|
class ArticleForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
Loading…
Reference in New Issue