简体   繁体   English

获取查询中所有值的属性 sqlalchemy

[英]Get attributes for all values in a query sqlalchemy

My database model is below: Users -->我的数据库 model 如下:用户 -->

class Users(db.Model):
   __tablename__ = 'users'
   id = db.Column(db.Integer(), primary_key=True)
   name = db.Column(db.String(length=100), nullable=False, unique=False)
   email = db.Column(db.String(length=30), nullable=False, unique=True)
   password_hash = db.Column(db.String(length=30),nullable=False,unique=False)
   isCorrect= db.Column(db.Boolean(),nullable=False, unique=False)

I'm trying to query specifically all values of isCorrect for True and access the name and email of the query.我正在尝试专门查询 isCorrect 的所有值是否为 True 并访问查询的名称email 。 Here is my query below:以下是我的查询:

check = Users.query.filter_by(isCorrect=True)

How can I add an additional method at the end of my query to get the name and email values?如何在查询末尾添加其他方法来获取名称和 email 值?

I tried looping through the query as shown below:我尝试遍历查询,如下所示:

for name in (Users.query.filter_by(isCorrect=True).all()):
   names = list(Users.query.filter_by(isTutor=True).first())

However, I got this error:但是,我收到了这个错误:

TypeError: 'Users' object is not iterable

I also tried converting the query into a list, but I got the same error.我也尝试将查询转换为列表,但我得到了同样的错误。

How can a list of all the attributes of a single query in SQLACHEMY (specifically flask extension)?如何在 SQLACHEMY 中列出单个查询的所有属性(特别是 flask 扩展名)?

Thanks for your help!谢谢你的帮助!

all() returns the results of the query as a Python list . all()将查询结果作为 Python list返回。

# Get all users whose isCorrect column is True
for user in Users.query.filter_by(isCorrect=True).all():
    # user is an instance of your Users class
    print(user.name)
    print(user.email)
    print(user.isCorrect)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM