[英]What type of result SQLAlchemy query() returns?
I've got a table in my database 我的数据库中有一张表
class Operator(db.Model):
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(100))
calls = db.relationship('Call', backref='operator', lazy='dynamic')
and I want to get all login
from this table. 我希望从此表中获取所有
login
信息。 So I do 所以我这样做
operators = db.session.query(models.Operator.login).all()
print(operators)
And I suppose to get a list of logins
but in fact I've got a list of tuples 我想要获得一个
logins
列表,但实际上我有一个元组列表
[('ivanov',), ('petrov',)]
Of course I can manage this data but I can't understand why it's formatted in that way? 当然我可以管理这些数据,但我无法理解为什么它以这种方式格式化? Why it has empty second element in every tuple?
为什么每个元组中都有空的第二个元素? Maybe I do something wrong and there is easier way to get list of
logins
? 也许我做错了什么,有更简单的方法来获取
logins
列表?
If you want to get a list of login
s something like this will do what you need: 如果你想获得一个
login
列表,这样就可以满足你的需求:
operators = db.session.query(Operator).all()
operators = [op.login for op in operators]
The tuple representation is the standard. 元组表示是标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.