Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

drf serializer general validate method

from rest_framework import serializers

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

    def validate(self, attrs):
        """
        Check that the start is before the stop.
        """
        if attrs['start'] > attrs['finish']:
            raise serializers.ValidationError("finish must occur after start")
        return attrs
Source by www.tomchristie.com #
 
PREVIOUS NEXT
Tagged: #drf #serializer #general #validate #method
ADD COMMENT
Topic
Name
7+1 =