2

I am using Flask and SqlAlchemy to build an API, but having a problem with fields.Url(), which I cannot figure out. I am getting the following error:

BuildError: ('storagenode_ep', MultiDict([('_sa_instance_state', <sqlalchemy.orm.state.InstanceState object at 0x4a76ad0>), ('id', 7), ('hostname', u'testNode1')]), None)

It works fine if I just return the id instead of trying to use field.Url(). The following simplified class demonstrates the issue:

storagenode_fields = {
    'hostname': fields.String,
    'uri': fields.Url('storagenode_ep')
    # 'id': fields.String
}

class StorageNodeAPI(Resource):
    @marshal_with(storagenode_fields)
    def get(self, storagenode_id):
        return StorageNode.query.filter_by(id=storagenode_id).first()

api.add_resource(StorageNodeAPI, '/api/storagenodes/<string:storagenode_id>', endpoint='storagenode_ep')

I have read this to better understand how endpoints, but I am still stuck.

The StorageNode class is defined as follows:

class StorageNode(Base):
    __tablename__ = 'storagenode'
    id = Column(Integer, primary_key=True)
    hostname = Column(String(150), unique=True)
Community
  • 1
  • 1
ranskis
  • 29
  • 1
  • 3

1 Answers1

0

You need to assign 'storagenode_ep' before returning it

Solved similar problem with help from How to add fields url for nested output fields in flask restful

But if you are using Blueprints, add "." to the fields.Url eg 'uri': fields.Url('.storagenode_ep')

ref https://github.com/flask-restful/flask-restful/issues/266#issuecomment-46678118

Community
  • 1
  • 1
AmaChefe
  • 395
  • 3
  • 8