summaryrefslogtreecommitdiff
path: root/docs/topics/forms
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@sixmedia.com>2014-02-04 01:31:27 +0700
committerTim Graham <timograham@gmail.com>2014-02-08 04:59:09 -0500
commit8847a0c601e4261823b1726b2db73eec2ac17940 (patch)
treead62fc5aca51cc9446ae68d7033691be68c87940 /docs/topics/forms
parent65131911dba08dcc1451d71ae4d5101724d722f6 (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/topics/forms')
-rw-r--r--docs/topics/forms/modelforms.txt19
1 files changed, 18 insertions, 1 deletions
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
---------------------