What is the difference between using a field validator or validating the model using clean?

3

Assuming I have a Django model like the following:

class Comprobante(models.Model):
    punto_venta = models.IntegerField(blank=True)

And I want to validate the model and particularly that punto_venta is a value from 1 to 9999. I understand that there are two ways:

Use a validator in the field

def punto_venta_validate(value):
    if not value:
        raise ValidationError(_('El punto de venta es obligatorio'))
    if value <1 or value > 9999:
        raise ValidationError(_('El punto de venta debe debe ser un valor entre 1 y 9999'))

class Comprobante(models.Model):
    punto_venta = models.IntegerField(blank=True, validators=[punto_venta_validate]))

Validate in the event clean() of the model

def clean(self):
    punto_venta_validate(self.punto_venta)

The only visible difference is that a ValidationError when validating the field by using validators , will display the message next to the field in the administration interface, when we use clean() to validate, I see that the error appears on all fields. Eventually with clean() we could also validate multiple conditions and each error add it to a list, in this way we could show all the errors of each field, so it would not be a difference between both methods either. So: What is the difference between the two methods? Is it just a matter of how the ValidationError is shown or is there something else that is slipping away?

    
asked by Patricio Moracho 01.05.2018 в 06:18
source

1 answer

4

Effectively as I mentioned in the comment:

  • The validadores only validate the entry, they do not return the improved format, that is, it can not be modified. If the entry is not valid, it will only generate a ValidationError .

  • The Clean() methods validate and return a value that could be a slightly modified value according to the requirements.

  

A validator is simply an object or callable function that takes a value and simply does not return anything if the value is valid or generates a ValidationError if it is not. (You can find this in the documentation: Here )    The return value is simply ignored.

     

If you want to be able to modify the value, you can use the clean_ field, in the forms as described here .

Django will first run the built-in field validators (by default), then its custom field validators (using validators=[su_validador] in its models). Then, Django will execute the methods clean() and clean<campo>() .

I think one of the main differences between the methods validator and% clean_<campo>() is that the latter is only intended for forms. And the validator can be used both for your forms and for your models (and, therefore, it will also be used, for example in the administration interface).

Also, overriding the clean_<campo>() method is the recommended way to validate data against elements in your database.

Note: You can also apply the same validator to your form fields if you only want it there and not in your entire application.

  

Here is an interesting documentation on Validation (Translate from English):

     
    
answered by 01.05.2018 / 21:50
source