17

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.

2 Answers 2

24

I don't have slugify to test, but this is probably what you are looking for:

artist = Artist.query.add_columns(slugify(Artist.Name).label("name_slug")).all()
Sign up to request clarification or add additional context in comments.

Comments

1

Similarly with select statements: stmt = sa.select(Artist.Age)

new_stmt = stmt.add_columns(Artist.Name)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.