I have a User model where users can have the same name. I want to get the email addresses of all the users with a given name. However, I have to do resu
There is a way to return specific columns from a filter_by query using the values method. As follows:
emails = [r[0] for r in db.session.query(my_table).filter_by(name=name).values('email')]
or:
emails = [r[0] for r in User.query.filter_by(name=name).values('email')]
values() takes any number of field names as parameters and returns a generator that has tuples with each value from each field name. Using a list comprehension to take the first item of that tuple emails will then be a list of plain string email addresses.