1

I have two models in my flask / restless app: Blueprint and Workload The Blueprint should have a collection of Workloads.

Here are the models:

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'])

What is the right syntax, either with curl or with python, to add a Workload instance to Blueprint's 'workloads' collection?

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167
  • Hi Eugene - please avoid adding tags to the title of your question as this does not help identify or highlight your question; instead use the tagging system. – Burhan Khalid Jan 28 '15 at 04:34

1 Answers1

1

Finally figured the answer: to add a Workload to Blueprint as a child: curl --request PATCH -H "Content-Type: application/json" -d '{ "blueprint": { "id": 1 } }' http://127.0.0.1:5000/api/workload/1 Given the Model definitions above, this does exactly what's needed. Then, when we run curl -i -X GET -H "Accept: application/json" http://127.0.0.1:5000/api/blueprint/1 we get a specific Blueprint along with a list of Workloads that belong to it

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167