内容简介:翻译自:https://stackoverflow.com/questions/37133774/how-can-i-select-only-one-column-using-sqlalchemy
我想仅使用“where子句”从我的数据库中选择(并返回)一个字段.代码是:
from sqlalchemy.orm import load_only @application.route("/user", methods=['GET', 'POST']) def user(): user_id = session.query(User, User.validation==request.cookies.get("validation")).options(load_only("id")) session.commit() return user_id
这失败了,回溯是:
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/Library/Python/2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request response = self.make_response(rv) File "/Library/Python/2.7/site-packages/flask/app.py", line 1577, in make_response rv = self.response_class.force_type(rv, request.environ) File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app return _run_wsgi_app(*args) File "/Library/Python/2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app app_rv = app(environ, start_response) TypeError: 'Query' object is not callable
如何选择并返回“id”列?我也尝试了其他几种方法,但也失败了. “load_only”是正确的选项吗?
Query
对象接受要作为位置参数进行查询的实体,因此只需将其传递给User.id:
user_id = session.query(User.id).\ filter(User.validation == request.cookies.get("validation")).\ scalar()
如果没有找到行,则
scalar()
返回第一个结果的第一个元素或None.它为多行引发MultipleResultsFound异常.
load_only()
表示只应加载实体的给定的基于列的属性,并且将延迟所有其他属性.如果您以后确实需要整个User模型对象,那么这可能就是您的选择.在这种情况下,您的原始查询必须更改为:
user = session.query(User).\ filter(User.validation == request.cookies.get("validation")).\ options(load_only("id")).\ one()
one()
只返回一个结果或引发异常(0或多于1个结果).如果您接受“无”作为“未找到用户”的有效返回值,请使用
one_or_none()
.
请注意,谓词(WHERE子句的条件)不应作为实体传递给Query对象,而是与
filter()
一起添加.
最重要的是,Flask中的视图期望您返回以下其中一个:
>一个有效的响应对象
>一个字符串
> a(响应,状态,标题)元组
>一个WSGI应用程序
除了作为WSGI应用程序的响应对象,字符串或元组之外,该机器还会处理任何其他内容.在原始代码中,由于缺少对scalar()等的调用,因此返回了Query对象,然后将其视为WSGI应用程序.
翻译自:https://stackoverflow.com/questions/37133774/how-can-i-select-only-one-column-using-sqlalchemy
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用feature Importance进行特征选择
- Qt QSS 属性选择器使用详解
- Django 表单模型选择框如何使用分组
- 使用 Tensorflow 实现口算检查器(一):模型选择
- 如何使用 React 构建自定义日期选择器(1)
- 如何使用 React 构建自定义日期选择器(3)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Effective JavaScript
David Herman / Addison-Wesley Professional / 2012-12-6 / USD 39.99
"It's uncommon to have a programming language wonk who can speak in such comfortable and friendly language as David does. His walk through the syntax and semantics of JavaScript is both charming and h......一起来看看 《Effective JavaScript》 这本书的介绍吧!