如果使用蓝图,`fields.Url('some_resource')` 不会在末尾附加 `id`
`fields.Url('some_resource')` doesn't append the `id` at the end if using blueprints
我正在使用蓝图,我用这条线来创建蓝图:
api_bp = Blueprint('api', __name__)
并添加 some_resources
并通过以下行注册蓝图:
rest_api = Api()
rest_api.init_app(api_bp)
rest_api.add_resource(SomeResourceApi, '/some_resources', '/some_resources/<int:some_resource_id>', endpoint='some_resources')
app.register_blueprint(api_bp, url_prefix='/some/api/v1.0')
用于 marshal()
的字段字典有 'uri': fields.Url('api.some_resources'),
我收到的输出是 "uri": "/some/api/v1.0/some_resources"
在我的回复中。 请注意,id 没有附加在 url.
的末尾
只是为了确保我将字段字典中的行 'uri': fields.Url('api.some_resources'),
替换为 'id': fields.Integer
,在这种情况下,整数 ID 在响应中正确返回。
此外,我已经用装饰器 @marshal_with
和方法 marshal()
进行了测试,这没有任何区别。
现在,我通过让我的字段字典有一个额外的字段来完成我的工作:id
的值 fields.Integer
包含实际的 id,所以输出有这两个共同给出完整地址的响应字段:
"uri": "/some/api/v1.0/some_resources",
"id": 11
虽然这达到了目的,但看起来有点老套。
当使用 fields.Url()
时,id 没有附加在响应中,我做错了什么吗?
库版本详细信息:
烧瓶==1.0.2
Flask-RESTful==0.3.6
Flask-SQLAlchemy==2.3.2
SQLAlchemy==1.2.10
对应的github问题:https://github.com/flask-restful/flask-restful/issues/774
我认为您不应该通过同一资源 class 路由对项目列表 (/some_resources
) 和单个项目 (/some_resources/:id
) 的详细信息的请求。
fields.Url
只是在后台调用 flask's url_for()
并且可能只是使用它找到的第一个匹配资源路径。也许如果你交换传递给 add_resource
的路径的顺序它会起作用。
我正在使用蓝图,我用这条线来创建蓝图:
api_bp = Blueprint('api', __name__)
并添加 some_resources
并通过以下行注册蓝图:
rest_api = Api()
rest_api.init_app(api_bp)
rest_api.add_resource(SomeResourceApi, '/some_resources', '/some_resources/<int:some_resource_id>', endpoint='some_resources')
app.register_blueprint(api_bp, url_prefix='/some/api/v1.0')
用于 marshal()
的字段字典有 'uri': fields.Url('api.some_resources'),
我收到的输出是 "uri": "/some/api/v1.0/some_resources"
在我的回复中。 请注意,id 没有附加在 url.
只是为了确保我将字段字典中的行 'uri': fields.Url('api.some_resources'),
替换为 'id': fields.Integer
,在这种情况下,整数 ID 在响应中正确返回。
此外,我已经用装饰器 @marshal_with
和方法 marshal()
进行了测试,这没有任何区别。
现在,我通过让我的字段字典有一个额外的字段来完成我的工作:id
的值 fields.Integer
包含实际的 id,所以输出有这两个共同给出完整地址的响应字段:
"uri": "/some/api/v1.0/some_resources",
"id": 11
虽然这达到了目的,但看起来有点老套。
当使用 fields.Url()
时,id 没有附加在响应中,我做错了什么吗?
库版本详细信息:
烧瓶==1.0.2
Flask-RESTful==0.3.6
Flask-SQLAlchemy==2.3.2
SQLAlchemy==1.2.10
对应的github问题:https://github.com/flask-restful/flask-restful/issues/774
我认为您不应该通过同一资源 class 路由对项目列表 (/some_resources
) 和单个项目 (/some_resources/:id
) 的详细信息的请求。
fields.Url
只是在后台调用 flask's url_for()
并且可能只是使用它找到的第一个匹配资源路径。也许如果你交换传递给 add_resource
的路径的顺序它会起作用。