这是我的代码,它正在工作(返回按难度排序的所有问题):
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
但我认为我必须优化这段代码。
是否有可能更改具有order_by
和我的函数difficulty()
的代码?我的函数返回一个数字。我试过这样的方法:
但是我收到错误TypeError: 'NoneType' object is not callable
。在
Hybrid attributes是同时充当Python属性和SQL表达式的特殊方法。只要您的
difficulty
函数可以用SQL表示,它就可以像普通列一样用于过滤和排序。在例如,如果将难度计算为问题的鹦鹉数量,乘以10(如果问题超过30天),则可以使用:
并查询:
^{pr2}$相关问题 更多 >
编程相关推荐