所以我想为模板视图获取一些模型对象,但我需要先调用每个选定元素的计算热度或计算排名。
models = Model.objects.a_bunch_of_filtering_sorting()
那么有什么快速的方法来做到这一点呢?
也许重申名单?
for model in models: # Surely it can't be this
model.the_method()
我尝试做一些自定义 SQL 的东西,但它不允许任何方法调用或列表排序太早
sortValue = ORDER BY ....
cursor = connection.cursor()
cursor.execute("""
SELECT d.field1, d.field2 ........
# unfortunately method calls aren't allowed in here
FROM Model_model d
GROUP BY 1
%s""" % sortValue)
for row in cursor.fetchall():
d = self.model(id=row[0] .... )
d.the_method() # wont work, the list is already
# ordered so we are calculating a sorting key one step behind
那么在对所有 django 模型(最首选的方式)进行排序并将它们发布到模板之前,我可以采取哪些可能的步骤来调用方法
谢谢