【发布时间】:2018-12-19 06:52:07
【问题描述】:
我正在 python flask-restful 中创建一个 rest-api,它将采用字符串参数(例如,'Retailer')并在属性 id 上自动为其生成 crud 模板。零售商还有一些我使用 ORM 迁移的属性。现在我需要使用 str 类型的类名“Retailer”并将其转换为资源并自动生成 crud。我的代码如下。
if __name__ != '__main__':
from flask import Flask,jsonify
from flask_restful import Resource, Api, reqparse
import parse_json
import create_models
app = Flask(__name__)
api = Api(app)
temp = parse_json.class_name
def get(self,id):
#code to be written here
vars()[temp] = type(temp,(Resource,),{'get' : get})
api.add_resource(vars()[temp], '/{0}/<int:id>'.format(temp))
app.run(port = 5000)
我只在该资源上创建了 get 方法,但不知道在其中写入什么来通过其 id 获取所有属性。请帮忙!
【问题讨论】:
-
只需调用处理所选请求的函数
-
我正在动态地将get方法添加到这个资源中。这是处理请求的地方。
-
使用 id 作为参数调用模型处理程序
标签: python-3.x rest flask-restful