1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #32347 -- Made ModelChoiceField include the value in ValidationError for invalid_choice.

This commit is contained in:
Jerin Peter George
2021-01-26 12:05:20 +05:30
committed by Mariusz Felisiak
parent 42e8cf47c7
commit 1adc09064f
5 changed files with 32 additions and 2 deletions

View File

@@ -1219,6 +1219,9 @@ generating choices. See :ref:`iterating-relationship-choices` for details.
* Validates that the given id exists in the queryset.
* Error message keys: ``required``, ``invalid_choice``
The ``invalid_choice`` error message may contain ``%(value)s``, which will
be replaced with the selected choice.
Allows the selection of a single model object, suitable for representing a
foreign key. Note that the default widget for ``ModelChoiceField`` becomes
impractical when the number of entries increases. You should avoid using it
@@ -1307,6 +1310,11 @@ generating choices. See :ref:`iterating-relationship-choices` for details.
def label_from_instance(self, obj):
return "My Object #%i" % obj.id
.. versionchanged:: 4.0
Support for containing ``%(value)s`` in the ``invalid_choice`` error
message was added.
``ModelMultipleChoiceField``
----------------------------

View File

@@ -135,7 +135,11 @@ File Uploads
Forms
~~~~~
* ...
* :class:`~django.forms.ModelChoiceField` now includes the provided value in
the ``params`` argument of a raised
:exc:`~django.core.exceptions.ValidationError` for the ``invalid_choice``
error message. This allows custom error messages to use the ``%(value)s``
placeholder.
Generic Views
~~~~~~~~~~~~~