【问题标题】:Creating dynamic resources in flask restful and performing crud operations在 flask restful 中创建动态资源并执行 crud 操作
【发布时间】: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


【解决方案1】:

我很好理解,例如根据选定的 id 从您的数据库中检索数据

cur = con.cursor()
cur.execute("SELECT * FROM dataset where id='"+id+"'")
data = cur.fetchall()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2013-02-17
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多