diff options
| author | Loic Bistuer <loic.bistuer@sixmedia.com> | 2014-02-04 01:31:27 +0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-02-08 04:59:09 -0500 |
| commit | 8847a0c601e4261823b1726b2db73eec2ac17940 (patch) | |
| tree | ad62fc5aca51cc9446ae68d7033691be68c87940 /docs | |
| parent | 65131911dba08dcc1451d71ae4d5101724d722f6 (diff) | |
Fixed #16192 -- Made unique error messages in ModelForm customizable.
Overriding the error messages now works for both unique fields, unique_together
and unique_for_date.
This patch changed the overriding logic to allow customizing NON_FIELD_ERRORS
since previously only fields' errors were customizable.
Refs #20199.
Thanks leahculver for the suggestion.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/exceptions.txt | 9 | ||||
| -rw-r--r-- | docs/ref/models/fields.txt | 8 | ||||
| -rw-r--r-- | docs/ref/models/instances.txt | 4 | ||||
| -rw-r--r-- | docs/ref/models/options.txt | 5 | ||||
| -rw-r--r-- | docs/releases/1.7.txt | 8 | ||||
| -rw-r--r-- | docs/topics/forms/modelforms.txt | 19 |
6 files changed, 48 insertions, 5 deletions
diff --git a/docs/ref/exceptions.txt b/docs/ref/exceptions.txt index 956ee5a188..c84dd87ed3 100644 --- a/docs/ref/exceptions.txt +++ b/docs/ref/exceptions.txt @@ -124,6 +124,15 @@ ValidationError :ref:`Model Field Validation <validating-objects>` and the :doc:`Validator Reference </ref/validators>`. +NON_FIELD_ERRORS +~~~~~~~~~~~~~~~~ +.. data:: NON_FIELD_ERRORS + +``ValidationError``\s that don't belong to a particular field in a form +or model are classified as ``NON_FIELD_ERRORS``. This constant is used +as a key in dictonaries that otherwise map fields to their respective +list of errors. + .. currentmodule:: django.core.urlresolvers URL Resolver exceptions diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 22fadd26c3..c6a5311760 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -233,8 +233,12 @@ field will raise. Pass in a dictionary with keys matching the error messages you want to override. Error message keys include ``null``, ``blank``, ``invalid``, ``invalid_choice``, -and ``unique``. Additional error message keys are specified for each field in -the `Field types`_ section below. +``unique``, and ``unique_for_date``. Additional error message keys are +specified for each field in the `Field types`_ section below. + +.. versionadded:: 1.7 + +The ``unique_for_date`` error message key was added. ``help_text`` ------------- diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 6bc6917a9e..2177bc6a59 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -151,8 +151,8 @@ access to more than a single field:: Any :exc:`~django.core.exceptions.ValidationError` exceptions raised by ``Model.clean()`` will be stored in a special key error dictionary key, -``NON_FIELD_ERRORS``, that is used for errors that are tied to the entire model -instead of to a specific field:: +:data:`~django.core.exceptions.NON_FIELD_ERRORS`, that is used for errors +that are tied to the entire model instead of to a specific field:: from django.core.exceptions import ValidationError, NON_FIELD_ERRORS try: diff --git a/docs/ref/models/options.txt b/docs/ref/models/options.txt index ba07dfaeb9..ae2d7def0f 100644 --- a/docs/ref/models/options.txt +++ b/docs/ref/models/options.txt @@ -321,6 +321,11 @@ Django quotes column and table names behind the scenes. :class:`~django.db.models.ManyToManyField`, try using a signal or an explicit :attr:`through <ManyToManyField.through>` model. + .. versionchanged:: 1.7 + + The ``ValidationError`` raised during model validation when the + constraint is violated has the ``unique_together`` error code. + ``index_together`` ------------------ diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index a51c3dc981..f9d3a7e795 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -484,6 +484,14 @@ Forms that maps fields to their original errors, complete with all metadata (error code and params), the latter returns the errors serialized as json. +* It's now possible to customize the error messages for ``ModelForm``’s + ``unique``, ``unique_for_date``, and ``unique_together`` constraints. + In order to support ``unique_together`` or any other ``NON_FIELD_ERROR``, + ``ModelForm`` now looks for the ``NON_FIELD_ERROR`` key in the + ``error_messages`` dictionary of the ``ModelForm``’s inner ``Meta`` class. + See :ref:`considerations regarding model's error_messages + <considerations-regarding-model-errormessages>` for more details. + Internationalization ^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index e98fcb0344..1cac2d6e7e 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -259,7 +259,9 @@ The model's ``clean()`` method will be called before any uniqueness checks are made. See :ref:`Validating objects <validating-objects>` for more information on the model's ``clean()`` hook. -Considerations regarding fields' ``error_messages`` +.. _considerations-regarding-model-errormessages: + +Considerations regarding model's ``error_messages`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error messages defined at the @@ -267,12 +269,27 @@ Error messages defined at the :ref:`form Meta <modelforms-overriding-default-fields>` level always take precedence over the error messages defined at the :attr:`model field <django.db.models.Field.error_messages>` level. + Error messages defined on :attr:`model fields <django.db.models.Field.error_messages>` are only used when the ``ValidationError`` is raised during the :ref:`model validation <validating-objects>` step and no corresponding error messages are defined at the form level. +.. versionadded:: 1.7 + +You can override the error messages from ``NON_FIELD_ERRORS`` raised by model +validation by adding the :data:`~django.core.exceptions.NON_FIELD_ERRORS` key +to the ``error_messages`` dictionary of the ``ModelForm``’s inner ``Meta`` class:: + + class ArticleForm(ModelForm): + class Meta: + error_messages = { + NON_FIELD_ERRORS: { + 'unique_together': "%(model_name)s's %(field_labels)s are not unique.", + } + } + The ``save()`` method --------------------- |
