如何在 big Flask 中访问 api.url_for-Restful API
How to access api.url_for in big Flask-Restful API
我正在使用 Flask-Restful 开发 Restful API,资源比我想保留在 app.py 中的资源多。所以我应用了资源中建议的 project structure. Now I'd like to access api.url_for() 来生成一些链接,但似乎我必须 from app import api
才能做到这一点。
为了避免循环导入,我目前的解决方案是进行惰性导入。但是有更好的方法,对吧?
app.py
:
from flask import Flask
from flask_restful import Api
from myapi.resources.foo import Foo
from myapi.resources.bar import Bar
from myapi.resources.baz import Baz
app = Flask(__name__)
api = Api(app)
api.add_resource(Foo, '/Foo', '/Foo/<str:id>')
api.add_resource(Bar, '/Bar', '/Bar/<str:id>')
api.add_resource(Baz, '/Baz', '/Baz/<str:id>')
resource/foo.py
(分别为bar.py
):
from flask_restful import Resource
from bar import Bar
class Foo(Resource):
def get(self):
from app import api
related = api.url_for(Bar, foo=self.id)
return {'Foo':self.id, 'related_bar':related}, 200
def post(self):
pass
您可以将导入 向下 移动到 api = Api(app)
行下方:
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
api = Api(app)
from myapi.resources.foo import Foo
from myapi.resources.bar import Bar
from myapi.resources.baz import Baz
api.add_resource(Foo, '/Foo', '/Foo/<str:id>')
api.add_resource(Bar, '/Bar', '/Bar/<str:id>')
api.add_resource(Baz, '/Baz', '/Baz/<str:id>')
现在 api
名称已定义,您可以安全地在 resources
模块中导入 from app import api
:
from flask_restful import Resource
from app import api
from bar import Bar
class Foo(Resource):
def get(self):
related = api.url_for(Bar, foo=self.id)
return {'Foo':self.id, 'related_bar':related}, 200
def post(self):
pass
请参阅 Flask 文档中的 Larger Applications pattern。
我正在使用 Flask-Restful 开发 Restful API,资源比我想保留在 app.py 中的资源多。所以我应用了资源中建议的 project structure. Now I'd like to access api.url_for() 来生成一些链接,但似乎我必须 from app import api
才能做到这一点。
为了避免循环导入,我目前的解决方案是进行惰性导入。但是有更好的方法,对吧?
app.py
:
from flask import Flask
from flask_restful import Api
from myapi.resources.foo import Foo
from myapi.resources.bar import Bar
from myapi.resources.baz import Baz
app = Flask(__name__)
api = Api(app)
api.add_resource(Foo, '/Foo', '/Foo/<str:id>')
api.add_resource(Bar, '/Bar', '/Bar/<str:id>')
api.add_resource(Baz, '/Baz', '/Baz/<str:id>')
resource/foo.py
(分别为bar.py
):
from flask_restful import Resource
from bar import Bar
class Foo(Resource):
def get(self):
from app import api
related = api.url_for(Bar, foo=self.id)
return {'Foo':self.id, 'related_bar':related}, 200
def post(self):
pass
您可以将导入 向下 移动到 api = Api(app)
行下方:
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
api = Api(app)
from myapi.resources.foo import Foo
from myapi.resources.bar import Bar
from myapi.resources.baz import Baz
api.add_resource(Foo, '/Foo', '/Foo/<str:id>')
api.add_resource(Bar, '/Bar', '/Bar/<str:id>')
api.add_resource(Baz, '/Baz', '/Baz/<str:id>')
现在 api
名称已定义,您可以安全地在 resources
模块中导入 from app import api
:
from flask_restful import Resource
from app import api
from bar import Bar
class Foo(Resource):
def get(self):
related = api.url_for(Bar, foo=self.id)
return {'Foo':self.id, 'related_bar':related}, 200
def post(self):
pass
请参阅 Flask 文档中的 Larger Applications pattern。