crazyrohila
crazyrohila

Reputation: 616

dynamic field query in peewee

I have modal defined and able to get result with select query:

country = 'usa'
User.select(User.email, User.username).where(User.country==country)

I want to filter this dynamic field name, like:

field = 'country'
country = 'usa'
User.select(User.email, User.username).where(User[field]==country)

Is it possible to do that?

Upvotes: 1

Views: 1026

Answers (1)

Menglong Li
Menglong Li

Reputation: 2255

Try attrgetter:

from operator import attrgetter


field = 'country'
country = 'usa'
User.select(User.email, User.username).where(attrgetter(field)(User)==country)

Upvotes: 2

Related Questions