どうやらこれについていくつかの議論があり、CampToCampは一般的な解決策との統合提案を投稿しました。彼らのブログにもいくつかの議論があります。
私はまだ彼らの解決策を試していませんが、_generate_order_by()
メソッドをオーバーライドすることによって、1つのフィールドに対して特定の解決策を作成しました。ユーザーが列ヘッダーをクリックするたびに、適切な句_generate_order_by()
を生成しようとします。実際にSQLサブクエリを句に入れて、機能フィールドの値を再現ORDER BY
できることがわかりました。ORDER BY
例として、各製品の最初のサプライヤの名前を表示する機能フィールドを追加しました。
def _product_supplier_name(self, cr, uid, ids, name, arg, context=None):
res = {}
for product in self.browse(cr, uid, ids, context):
supplier_name = ""
if len(product.seller_ids) > 0:
supplier_name = product.seller_ids[0].name.name
res[product.id] = supplier_name
return res
その列で並べ替えるために、かなりファンキーなSQLで上書きします。_generate_order_by()
その他の列については、通常のコードに委任します。
def _generate_order_by(self, order_spec, query):
""" Calculate the order by clause to use in SQL based on a set of
model fields. """
if order_spec != 'default_partner_name':
return super(product_product, self)._generate_order_by(order_spec,
query)
return """
ORDER BY
(
select min(supp.name)
from product_supplierinfo supinf
join res_partner supp
on supinf.name = supp.id
where supinf.product_id = product_product.id
),
product_product.default_code
"""