Flask 烧瓶+;云数据存储:路由捕获具有祖先关系的实体ID的参数

Flask 烧瓶+;云数据存储:路由捕获具有祖先关系的实体ID的参数,flask,google-cloud-firestore,google-cloud-datastore,Flask,Google Cloud Firestore,Google Cloud Datastore,谷歌云数据存储使用键来识别和查询实体。这与Flask.route在查询实体时的url参数配合得很好: from google.cloud import datastore client = datastore.Client() @app.route('/post/<post_id>', methods=['GET', 'POST']) def post(post_id): post = client.get(client.key('Post', post_id))

谷歌云数据存储使用键来识别和查询
实体
。这与Flask.route在查询实体时的url参数配合得很好:

from google.cloud import datastore
client = datastore.Client()

@app.route('/post/<post_id>', methods=['GET', 'POST'])
def post(post_id):
    post = client.get(client.key('Post', post_id)) 
    return client
但是如果
Post
键实际上具有
祖先,如下面所示,该怎么办

Key('User', 'user123', 'Post', 'post123')
这意味着烧瓶路径将不再处理
/post/
,因为
post\u id
沿线不代表整个实体


这种情况的可能解决方案是什么?

与目录文件系统一样,实体在云数据存储中是分层结构的。这种类型的结构使组织数据时更加整洁

发件人:

标识实体的完整密钥由一系列种类标识符对组成,指定其祖先路径,并以实体本身的路径终止:

对象类别->种类

一个对象->实体

对象的单个数据->属性

对象的唯一ID->

我相信您仍然可以使用相同的路径,但需要调整代码,首先使用其祖先路径查询您感兴趣的post ID

这是我尝试过的工作代码,您可以修改它以适应您的需要

from flask import Flask, render_template, redirect, request
from google.cloud import datastore
from markupsafe import escape

# Starting Datastore Client.
client = datastore.Client()

# Home page will do a redirect to the specified URL.
@app.route('/')
def hello():
    return redirect('/post/<post_id>', code=302)

# The URL that we are intestered in. 
@app.route('/post/<post_id>', methods=['GET', 'POST'])
def profile(post_id):

# Query the post ID using its ancestors. 

# Method is always 'GET' by default, I kept it 'GET' just for testing purposes.
if request.method == 'GET' and request.url == '<post_id>':
    return '{} is the post ID'.format(escape(post_id))
else:
    return '{} is the post ID'.format(escape(post_id))



# The actual Flask app.
app = Flask(__name__)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)
从flask导入flask,呈现\u模板,重定向,请求
从google.cloud导入数据存储
从markupsafe导入转义
#正在启动数据存储客户端。
client=datastore.client()
#主页将重定向到指定的URL。
@应用程序路径(“/”)
def hello():
返回重定向('/post/',代码=302)
#我们登录的URL。
@app.route('/post/',方法=['GET','post'])
def配置文件(post_id):
#使用帖子的祖先查询帖子ID。
#方法在默认情况下总是“GET”,我将其保留为“GET”只是为了测试。
如果request.method=='GET'和request.url='':
返回“{}是post ID”。格式(转义(post_ID))
其他:
返回“{}是post ID”。格式(转义(post_ID))
#实际的烧瓶应用程序。
app=烧瓶(名称)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(host='127.0.0.1',port=8080,debug=True)
虽然我知道这可能不会给你一个直接的答案,但我相信它会为你指明正确的方向。

考虑到子实体本身的复杂性,你如何查询它的祖先?这不是不可能吗?
from flask import Flask, render_template, redirect, request
from google.cloud import datastore
from markupsafe import escape

# Starting Datastore Client.
client = datastore.Client()

# Home page will do a redirect to the specified URL.
@app.route('/')
def hello():
    return redirect('/post/<post_id>', code=302)

# The URL that we are intestered in. 
@app.route('/post/<post_id>', methods=['GET', 'POST'])
def profile(post_id):

# Query the post ID using its ancestors. 

# Method is always 'GET' by default, I kept it 'GET' just for testing purposes.
if request.method == 'GET' and request.url == '<post_id>':
    return '{} is the post ID'.format(escape(post_id))
else:
    return '{} is the post ID'.format(escape(post_id))



# The actual Flask app.
app = Flask(__name__)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)