Serializers - HyperlinkedModelSerializer: Validate your attributes (fields) [closed]

3

In restframework how could you validate the fields?

I am using HyperlinkedModelSerializer . It happens that I have a model that specifies that for each type of identification document will have an exact number of digits, then I want it to be fulfilled that the amount entered by the user is equal to that amount established.

def validate(self, attrs):

    documento_identificacion = attrs.get('documento_identificacion', '')
    documentos_identificaciones = DocumentoIdentificacion.objects.filter(documento_identificacion = documento_identificacion)
    numero_documento_identificacion = attrs.get('numero_documento_identificacion', '')

    if documentos_identificaciones.numero_digito != len(numero_documento_identificacion):
        return serializers.ValidationError('No cumple con la cantidad de digitos establecidos')
    return attrs
    
asked by Roberto Gian Franco Carbonell 19.07.2016 в 19:46
source

0 answers