Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Bit late to this one, but I have been through the same issues myself, so using this as a note_to_self as much as anything.)</p> <p>You can specify <code>error_messages</code> on both models and modelforms. The keys you should / can use are defined <a href="https://docs.djangoproject.com/en/dev/ref/forms/fields/#built-in-field-classes" rel="nofollow">here</a> for the form fields. The issue seems to be (to me) the inter-relationship between forms and the related model, and which error message appears, and when. The key to this is understanding that forms and models are actually very loosely-coupled, and that there really is no magic happening. </p> <p>If you have a model field called 'quote', with a max_length of 140, and a modelform associated with this model, the error messages will work thus:</p> <ul> <li>If you don't <em>explicitly</em> add a max_length attribute to the modelform, and then validate the form (calling <code>is_valid()</code> or <code>errors</code>), the error message that comes back will be from the model.</li> <li>If you add a set of error_messages ('required','max_length') to the model, these will appear in the errors collection. </li> <li>If you add a set of error_messages to the modelform, they will not appear, as it is the model that is failing validation, not the form.</li> <li>If you then add a max_length attribute to the modelform, you will see the modelform errors surfacing (and overriding the model error_messages.)</li> </ul> <p>So - fairly simple in summary - the error messages map to the object that is being validated - which can be either the model or the modelform.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload