简体   繁体   中英

Sqlalchemy - add columns to a query

For example I am using the chinook database and I would like to convert the Name field into a slug. Slugify is a function from awesome-slugify.

Something like this in SQL

Select *, slugify(Name) as name_slug
from Artist

In sqlalchemy I have tried:

artist = Artist.query.add_columns(name_slug=slugify(Artist.Name)).all()

and

artist = Artist.query.add_columns(name_slug=[slugify(a.Name) for a in Artist.Name]).all()

I can generate a list of name slugs by doing to following in the terminal:

art = models.Artist.query.all()
name_slug = [slugify(a.Name) for a in art]
print(name_slug)

But I am not certain how to tie it all together.

我没有测试slugify,但这可能是你正在寻找的:

artist = Artist.query.add_columns(slugify(Artist.Name).label("name_slug")).all()

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