简体   繁体   中英

db object not iterable in flask

I'm searching for a record in Users db and then looping through all the records. The code is something like below.

email = "xyz@gmail.com"
data = Users.query.filter(email=email).first() 
for item in data:
    if item["age"] == 15:
        #do something

The above code throws error Users object not iterable . How can I loop through the records?

Because you only returned one row of data, it cannot be iterable. If your data is not null, you can use 'data.age' instead of 'item["age"]'

email = "xyz@gmail.com"
data = Users.query.filter(email=email).first() 
if data.age == 15:
   #do something
email = "xyz@gmail.com"
data = Users.query.filter(email=email).all() 

# age = data['age'] # You can use this also 


age = data.get('age') # Try This also
if age == 15:
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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