[Answered ]-How to validate a field with uppercase with django serializers

1๐Ÿ‘

โœ…

The following code does not validate the Status field.

It expects that the validator is named validate_Status, so with an uppercase S as well:

class DocumentSalesforceSerializer(serializers.Serializer):
    AccountId = serializers.CharField(required=True)
    ContactId = serializers.CharField(required=True)
    Status = serializers.CharField(required=True)
    StartDate = serializers.CharField(required=True)
    EndDate = serializers.CharField(required=True, allow_blank=True)
    Subject = serializers.CharField(required=True)

    def validate_Status(self, Status):
        if Status not in ("New", "In Progress", "On Hold", "Completed", "Closed", "Cannot Complete", "Canceled"):
            raise serializers.ValidationError("Invalid Status")
        return Status

Leave a comment