class MySerializer(serializers.Serializer): state = serializers.CharField(max_length=20, required=False) def validate_state(self, state): if not isinstance(state, str): raise serializers.ValidationError("state should be a string") return state
I want "state" to be only string format or raise exception when calling "serializer.is_valid(raise_exception=True)".
I dont use a model and also don't store it in a db, so i wont call update or create.
I tried this method(1) and overriding "validate" method of the serializer: I want the exception to trigger when i call "serializer.is_valid(raise_exception=True)"
def validate_state(self, state): if not isinstance(state, str): raise serializers.ValidationError("state should be a string") return state