This is the code I have and it is working (returns all problems ordered by difficulty):
def get_noteworthy_problems(self):
ACategory = aliased(Category)
AProblem = aliased(Problem)
all_prob = DBSession.query(AProblem).filter(
AProblem.parent_id == ACategory.id,
ACategory.parent_id == self.id)
noteworthy_problems = \
sorted(all_prob, key=lambda x: x.difficulty(), reverse=True)
return noteworthy_problems
But I think I must optimize this code.
Is there a possibility to change the code having order_by
and my function difficulty()
? My function returns a number. I tried something like:
result = DBSession.query(AProblem).filter(
AProblem.parent_id == ACategory.id,
ACategory.parent_id == self.id).order_by(
AProblem.difficulty().desc())
but I receive the error TypeError: 'NoneType' object is not callable
.