显然已经对此进行了一些讨论,并且CampToCamp发布了merge proposal以及一个通用解决方案.在their blog中也有一些讨论.
我还没有尝试过他们的解决方案,但是我确实通过重写_generate_order_by()方法为一个字段创建了一种特定的解决方案.每当用户单击列标题时,_generate_order_by()都会尝试生成适当的ORDER BY子句.我发现您实际上可以将SQL子查询放在ORDER BY子句中,以重现功能字段的值.
例如,我们添加了functional field,以显示每个产品的第一个供应商名称.
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 overrode _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
"""