5

在 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

上面在python中应用的sql代码

_order = ("CASE WHEN state='draft'  THEN 0",
          "WHEN state = 'assigned'  THEN 1",
          "ELSE 2 END, date desc")

在上述查询排序选择值在 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)

基于这个按选择值排序的顺序如何在 OpenERP 中应用?覆盖搜索方法也应用了相同的 sql 查询,但显示了相同的问题。

4

4 回答 4

2

尝试创建一个带有 store 属性的函数字段,当状态改变时加载函数。例如

def _get_state(cr, uid, ids,field_name, context=None):
    res={}
    for obj in self.browse(cr, uid, ids, context):
        res[obj.id] = (obj.state=='draft' and 0) or (obj.state=='assigned' and 1) or (obj.state=='cancel' and 2) or 3
    return res

_columns = {
current_state_num: fields.function(_get_state,string='Current state',type='integer',store={'your.current.model.name':(lambda cr, uid, ids, context:ids,['state'],20)})
}
_order = "current_state_num,date desc"
于 2013-09-01T16:34:06.477 回答
0

另一种解决方法是按状态排序并更改字段中状态的顺序:

'state': fields.selection([('draft','Draft'),('assigned','Assigned'), ('cancel','Cancelled'),('other' ....
...
_order = "state, date desc"

或者,您可以创建一个新的函数字段(例如 sort_priority)并使用类似的函数为优先级分配值,然后按该字段排序。

于 2013-08-27T07:23:34.030 回答
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

除了要订购的字段外,您还需要选择 id 字段。另一种可能性是更改视图上的默认过滤器。 在树视图上添加默认过滤器

于 2013-08-15T14:44:09.153 回答
0

如果你能读德语,这篇博文有一个很好的解决方案。这是它的改编版本:

class some_table(osv.Model):
    _name = 'some.table'
    _columns = {
        ...
        ...
        'state' : field.selection(...),
        ...
        ...

    def _generate_order_by(self, order_spec, query):
        "correctly orders state field if state is in query"
        order_by = super(some_table, self)._generate_order_by(order_spec, query)
        if order_spec and 'state ' in order_spec:
            state_column = self._columns['state']
            state_order = 'CASE '
            for i, state in enumerate(state_column.selection):
                state_order += "WHEN %s.state='%s' THEN %i " % (self._table, state[0], i)
            state_order += 'END '
            order_by = order_by.replace('"%s"."state" ' % self._table, state_order)
        return order_by
于 2013-12-02T23:10:18.833 回答