【问题标题】:how to add a child instance to the "many" end of one-to-many如何将子实例添加到一对多的“多”端
【发布时间】:2015-03-26 20:10:56
【问题描述】:

我的烧瓶/不安应用程序中有两个模型:蓝图和工作负载 蓝图应该有一个工作负载的集合。

以下是模型:

class Blueprint(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), unique=True)
    description = db.Column(db.String(250), unique=True)


class Workload(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), unique=True)
    description = db.Column(db.String(250), unique=True)
    image = db.Column(db.String(120), unique=True)
    flavor = db.Column(db.String(120), unique=True)
    blueprint_id = db.Column(db.Integer, db.ForeignKey('blueprint.id'))
    blueprint = db.relationship(Blueprint, backref='workloads')


db.create_all()

# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)

# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Blueprint, methods=['GET', 'PUT', 'POST', 'DELETE'])
manager.create_api(Workload, methods=['GET', 'PUT', 'POST', 'DELETE'])

使用 curl 或 python 将 Workload 实例添加到 Blueprint 的“workloads”集合的正确语法是什么?

【问题讨论】:

  • 嗨 Eugene - 请避免在您的问题标题中添加标签,因为这无助于识别或突出您的问题;而是使用标记系统。

标签: flask flask-restless


【解决方案1】:

终于找到了答案:将工作负载添加到蓝图作为一个孩子: curl --request PATCH -H "Content-Type: application/json" -d '{ "blueprint": { "id": 1 } } ' http://127.0.0.1:5000/api/workload/1 鉴于上面的模型定义,这正是需要的。然后,当我们运行 curl -i -X GET -H "Accept: application/json" http://127.0.0.1:5000/api/blueprint/1 时,我们会得到一个特定的蓝图以及属于它的工作负载列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多