Answer the question
In order to leave comments, you need to log in
How to access an attribute of an object dynamically?
Sorry, but I don't know what keywords to search for.
Let's say there is a code for getting data from the database and sorting it (schematically):
if sort_by == "date" : sort_by = Articles.created.desc()
Articles.select().order_by(sort_by).offset(offset).limit(limit)
if sort_by == "created" : sort_by = Articles.created.desc()
Articles.select().order_by(sort_by).offset(offset).limit(limit)
def get_all_data(Model, sort_by, offset, limit):
try:
sort_by = Model.%SORT_BY%.desc()
return Model.select().order_by(sort_by).offset(offset).limit(limit)
except:
raise Exception("xxx")
Answer the question
In order to leave comments, you need to log in
attr_name = 'created'
sort_by = getattr(Model, attr_name).desc()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question