Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use error_messages on models in Django
    primarykey
    data
    text
    <p>I understand form the documentation <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" rel="nofollow">http://docs.djangoproject.com/en/dev/ref/models/fields/</a> that you can add error_messages to a model field and supply your own dict of error messages. However, what are they keys of the dict you are supposed to pass?</p> <pre><code>class MyModel(models.Model): some_field = models.CharField(max_length=55, error_messages={'required': "My custom error"}) </code></pre> <p>If it is easier to do this on the modelform that is used that would also work, however. I would rather not have to create explicitly creating each field and their type again. This is what I was trying to avoid:</p> <pre><code>class MyModelForm(forms.ModelForm): some_field = forms.CharField(error_messages={'required' : 'Required error'}) </code></pre> <p><strong>Update 2:</strong> Test code used in my project</p> <p>My Model:</p> <pre><code>class MyTestModel(models.Model): name = models.CharField(max_length=127,error_messages={'blank' : 'BLANK','required' : 'REQUIRED'}) </code></pre> <p>My Form:</p> <pre><code>class EditTestModel(ModelForm): class Meta: model = MyTestModel </code></pre> <p>My View:</p> <pre><code>tf = EditTestModel({'name' : ''}) print tf.is_valid() # prints False print tf.full_clean() # prints None print tf # prints the form, with a &lt;li&gt; error list containg the error "This field is required" &lt;tr&gt;&lt;th&gt;&lt;label for="id_name"&gt;Name:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;input id="id_name" type="text" name="name" maxlength="127" /&gt;&lt;/td&gt;&lt;/tr&gt; </code></pre>
    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.
 

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