Django-rest pass validations in two fields

1

How to retrieve values from html elements in a custom validation with Django-rest?

How would django-rest be similar to the code below?

def clean(self):
    password1 = self.cleaned_data.get('password1')
    password2 = self.cleaned_data.get('password2')

    if password1 and password1 != password2:
        raise forms.ValidationError("Passwords don't match")

return self.cleaned_data

I have tried with passcode1 (self, date):

I've also tried with ( to_internal_value ), but I need the return as in validate_CAMPO . Example (Field: error msg)

    
asked by anonymous 05.01.2016 / 20:38

1 answer

2

To do any other validation that requires access to multiple fields, add a method named .validate() to its subclass Serializer . This method uses a single argument, which is a dictionary of field values. You should raise% w / o, if necessary, or just return the validated values. For example:

from rest_framework import serializers

class EventSerializer(serializers.Serializer):
    description = serializers.CharField(max_length=100)
    start = serializers.DateTimeField()
    finish = serializers.DateTimeField()

    def validate(self, data):
        """
        Check that the start is before the stop.
        """
        if data['start'] > data['finish']:
            raise serializers.ValidationError("finish must occur after start")
        return data

Translation of the excerpt from the documentation.

    
04.07.2016 / 21:13