我正在使用Flask-Restful和SQLAlchemy来开发一个API,并且遇到了一个我有解决方法的行为,但我不确定坚持使用这个解决方案是一个很好的长期策略。
相当标准的API,带有POST功能的List项目资源和带有GET功能的项目单一资源。我通过curl和web表单测试了POST函数,并收到以下错误消息:
BuildError: Could not build url for endpoint 'brand' with values ['_sa_instance_state']. Did you forget to specify values ['brand_symbol']?
奇怪的是,添加一个print语句(请参阅下面的Resource声明末尾附近的注释掉的print语句)会使错误消失。
我知道即使遇到错误信息
基于此,我认为问题可能在于POST return
语句。我会非常感激地提供任何帮助 - 我不想忍受一个片状的解决方法!
端点
api.add_resource(BrandListResource, '/brands', endpoint = 'brands')
api.add_resource(BrandResource, '/brands/<string:brand_symbol>', endpoint = 'brand')
资源
brand_fields = {
'id': fields.Integer,
'brand_symbol': fields.String,
'brand_name': fields.String,
'uri': fields.Url('brand', absolute=True)
}
class BrandResource(Resource):
@marshal_with(brand_fields)
def get(self, brand_symbol):
brand = db.session.query(Brand).filter(Brand.brand_symbol == brand_symbol).first()
if not brand:
abort(404, message="Brand {} doesn't exist".format(brand_symbol))
return brand
class BrandListResource(Resource):
@marshal_with(brand_fields)
def get(self):
brands = db.session.query(Brand).all()
return brands
@marshal_with(brand_fields)
def post(self):
parsed_args = parser.parse_args()
brand_symbol = parsed_args['brand_symbol']
brand_name = parsed_args['brand_name']
brand = db.session.query(Brand).filter(Brand.brand_symbol == brand_symbol).first()
if brand:
abort(404, message="Brand {} already exists".format(brand_symbol))
brand = Brand(brand_symbol=brand_symbol, brand_name=brand_name)
db.session.add(brand)
db.session.commit()
#print brand
return brand, 201
SQLAlchemy模型
class Brand(db.Model):
__tablename__ = 'brand'
id = db.Column(db.Integer, primary_key=True)
brand_symbol = db.Column(db.String(5), unique=True)
brand_name = db.Column(db.String(200), nullable=False)
def __init__(self, brand_symbol, brand_name):
self.brand_symbol = brand_symbol
self.brand_name = brand_name
def __repr__(self):
return '<brand_symbol {}>'.format(self.brand_symbol)
完整错误回溯
Traceback (most recent call last):
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 682, in wrapper
return marshal(data, self.fields, self.envelope), code, headers
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 640, in marshal
return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 57, in __init__
self.__update(*args, **kwds)
File "/Users/skavie/testproject/lib/python2.7/_abcoll.py", line 571, in update
for key, value in other:
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/__init__.py", line 639, in <genexpr>
for k, v in fields.items())
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask_restful/fields.py", line 307, in output
o = urlparse(url_for(endpoint, _external=self.absolute, **data))
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/helpers.py", line 332, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/app.py", line 1811, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/Users/skavie/testproject/lib/python2.7/site-packages/flask/helpers.py", line 322, in url_for
force_external=external)
File "/Users/skavie/testproject/lib/python2.7/site-packages/werkzeug/routing.py", line 1758, in build
raise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint 'brand' with values ['_sa_instance_state']. Did you forget to specify values ['brand_symbol']?
答案 0 :(得分:0)
提交SQLAlchemy会话时,brand
对象已过期。
stacktrace通过指出由于您要返回的对象中缺少某些信息而无法构建url来提供提示。
在这种情况下,brand
对象需要刷新,因此它将再次包含所有数据。您可以通过打印它来做到这一点,但是像这样使用会话的refresh()方法似乎要好一些:
...
db.session.commit()
db.session.refresh(brand)
return brand, 201
有一个小警告,但是,刷新方法的documentation在撰写本文时指出以下内容:
请注意,高度隔离的事务将返回与先前在同一事务中读取的值相同的值,而不管该事务外部的数据库状态如何变化-使用refresh()通常仅在非ORM SQL语句被使用时才有意义。在正在进行的事务中发出,或者如果自动提交模式已打开。
这意味着您要刷新的信息可能已过时,但是我想在这种情况下这几乎是理论上的。