How to use error_messages for models in Django

I understand the form of the document http://docs.djangoproject.com/en/dev/ref/models/fields/ that you can add error_messages to the model field and provide your own error messages. However, what are the dictation keys that you must pass?

class MyModel(models.Model): some_field = models.CharField(max_length=55, error_messages={'required': "My custom error"}) 

If it is easier to do on the model used model, which will also work. I would prefer not to create an explicit creation of each field and their type again. This is what I tried to avoid:

 class MyModelForm(forms.ModelForm): some_field = forms.CharField(error_messages={'required' : 'Required error'}) 

Update 2: Test code used in my project

My model:

 class MyTestModel(models.Model): name = models.CharField(max_length=127,error_messages={'blank' : 'BLANK','required' : 'REQUIRED'}) 

My form:

 class EditTestModel(ModelForm): class Meta: model = MyTestModel 

My view:

 tf = EditTestModel({'name' : ''}) print tf.is_valid() # prints False print tf.full_clean() # prints None print tf # prints the form, with a <li> error list containg the error "This field is required" <tr><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input id="id_name" type="text" name="name" maxlength="127" /></td></tr> 
+4
source share
3 answers

You are right, these documents are not very useful. This is the latest addition after all!

I assume the usual use is error_messages for ModelForms, so I would look here for a list of valid error keys for each field: http://docs.djangoproject.com/en/dev/ref/forms/fields/#error-messages

But, if you want to be really safe and not assume anything ...

The most reliable way now is to look at the source on django/db/models/fields/__init__.py , where you will see each of the default_error_messages that can be specified, and the actual calls to self.error_messages['invalid']

 # Field (base class) default_error_messages = { 'invalid_choice': _(u'Value %r is not a valid choice.'), 'null': _(u'This field cannot be null.'), 'blank': _(u'This field cannot be blank.'), } # AutoField default_error_messages = { 'invalid': _(u'This value must be an integer.'), } 

Here's a model validation document: http://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects

Update:

Just checked this in a shell session and it seems to work. What?

I just defined a simple model:

 class SubscriptionGroup(models.Model): name = models.CharField(max_length=255, error_messages={'blank': 'INVALID!!11', 'null': 'NULL11!'}) # shell >>> s = SubscriptionGroup() >>> s.full_clean() ValidationError: {'name': [u'INVALID!!11']} 
+4
source

(A bit late for this, but I myself experienced the same problems, so using it as note_to_self, like everyone else.)

You can specify error_messages for both models and models. The keys you must use are defined here for the form fields. It seems that the problem is with (to me) the relationship between the forms and the model associated with them, and what error message appears, and when. The key to this is the understanding that forms and models are actually very loosely coupled and that no magic is actually happening.

If you have a model field called "quote", with max_length 140 and the model associated with this model, the error messages will work like this:

  • If you do not explicitly add the max_length attribute to the model form, and then check the form (calling is_valid() or errors ), the error message that is returned will be from the model.
  • If you add the error_messages ('required', 'max_length') parameter set to the model, they will appear in the error collection.
  • If you add a set of error_messages parameters to the model form, they will not be displayed, since it is a model that does not perform validation, not the form.
  • If you then add the max_length attribute to the model form, you will see that model form errors induce (and override the error_messages model.)

So, it's pretty simple: error messages are mapped to the object being checked, which can be either a model or a model.

+3
source

I tried. This will not work if you defined it in models. You must define error_messages in your forms, e.g.

 name = forms.CharField(error_messages={'required': 'this field is required'}) 
+2
source

Source: https://habr.com/ru/post/1337049/


All Articles