1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #8760 -- Changed ModelMultipleChoiceField to use invalid_list as a error message key.

This commit is contained in:
David Smith
2020-03-05 21:53:16 +00:00
committed by Mariusz Felisiak
parent 7d8cdad6b7
commit ccf32aca44
5 changed files with 50 additions and 8 deletions

View File

@@ -2,7 +2,7 @@
Helper functions for creating Form classes from Django models
and database field objects.
"""
import warnings
from itertools import chain
from django.core.exceptions import (
@@ -15,6 +15,7 @@ from django.forms.utils import ErrorList
from django.forms.widgets import (
HiddenInput, MultipleHiddenInput, RadioSelect, SelectMultiple,
)
from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.text import capfirst, get_text_list
from django.utils.translation import gettext, gettext_lazy as _
@@ -1291,7 +1292,7 @@ class ModelMultipleChoiceField(ModelChoiceField):
widget = SelectMultiple
hidden_widget = MultipleHiddenInput
default_error_messages = {
'list': _('Enter a list of values.'),
'invalid_list': _('Enter a list of values.'),
'invalid_choice': _('Select a valid choice. %(value)s is not one of the'
' available choices.'),
'invalid_pk_value': _('%(pk)s” is not a valid value.')
@@ -1299,6 +1300,13 @@ class ModelMultipleChoiceField(ModelChoiceField):
def __init__(self, queryset, **kwargs):
super().__init__(queryset, empty_label=None, **kwargs)
if self.error_messages.get('list') is not None:
warnings.warn(
"The 'list' error message key is deprecated in favor of "
"'invalid_list'.",
RemovedInDjango40Warning, stacklevel=2,
)
self.error_messages['invalid_list'] = self.error_messages['list']
def to_python(self, value):
if not value:
@@ -1312,7 +1320,10 @@ class ModelMultipleChoiceField(ModelChoiceField):
elif not self.required and not value:
return self.queryset.none()
if not isinstance(value, (list, tuple)):
raise ValidationError(self.error_messages['list'], code='list')
raise ValidationError(
self.error_messages['invalid_list'],
code='invalid_list',
)
qs = self._check_values(value)
# Since this overrides the inherited ModelChoiceField.clean
# we run custom validators here
@@ -1333,8 +1344,8 @@ class ModelMultipleChoiceField(ModelChoiceField):
except TypeError:
# list of lists isn't hashable, for example
raise ValidationError(
self.error_messages['list'],
code='list',
self.error_messages['invalid_list'],
code='invalid_list',
)
for pk in value:
try: