ank*_*ita 5 python openerp postgresql-8.4
在 OpenERP 7 列表视图中,我想显示订单草稿中的状态值排序,分配和取消当前以 Asc 或 Desc 显示。但在我的情况下,我们需要在订单草稿、分配和取消状态中进行排序。基于在python文件中按顺序应用
例如在 SQL 中的代码 -
select state, date from object_name
ORDER BY CASE WHEN state = 'draft' THEN 0
WHEN state = 'assigned' THEN 1
WHEN state = 'cancel' THEN 2
ELSE 3
END, date desc
Run Code Online (Sandbox Code Playgroud)
以上sql代码在python中应用
_order = ("CASE WHEN state='draft' THEN 0",
"WHEN state = 'assigned' THEN 1",
"ELSE 2 END, date desc")
Run Code Online (Sandbox Code Playgroud)
在上面的查询排序选择值在 pg_admin 中工作但在 python 代码中它显示以下错误
Invalid "order" specified. A valid "order" specification is a comma-separated
list of valid field names (optionally followed by asc/desc for the direction)
Run Code Online (Sandbox Code Playgroud)
基于这个按选择值排序的顺序如何在OpenERP中应用?覆盖搜索方法也应用了相同的 sql 查询,但显示了相同的问题。
小智 0
一个可能的解决方案是从 python 函数执行查询本身。然后您可以从操作或其他方法调用该函数。例如
def _my_custom_search(self, cr, uid, object_name, context=None):
sql_req = """
select id, state, date from %s
ORDER BY CASE WHEN state = 'draft' THEN 0
WHEN state = 'assigned' THEN 1
WHEN state = 'cancel' THEN 2
ELSE 3
END, date desc""" %(object_name, )
cr.execute(sql_req)
res = cr.fetchall()
return res
Run Code Online (Sandbox Code Playgroud)
除了排序所依据的字段之外,您还需要选择 id 字段。另一种可能性是更改视图上的默认过滤器。 在树视图上添加默认过滤器