简体   繁体   中英

Select specific column value with sqlalchamey based on filter

Been messing around with SQLalchemy and Flask and have run up into a problem that I can't find good documentation on.

I need to query for the email of a specific user, but I can't figure the right combination of filter and select the specific column.

I've tried:

user = User.query.filter(User.username == currentuser).email

user = User.query(User.email).filter(User.username == currentuser)

user = User.query.filter(User.username == currentuser, User.email)

user = User.query.filter(User.email, User.username == currentuser)

But I'm not able to get the results I want.

Pretty sure

user = User.query.filter(User.username == currentuser).email

does not actually execute the query, you have to do something like

user = User.query.filter(User.username == currentuser).first().email

as per http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.first

Note, the statement above will fail if the user doesn't exist as first() will return None.

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