Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by selection in List view of OpenERP 7.0

In OpenERP 7 list view I want to shows the state value sort in the order draft, assigned and cancel currently shows in Asc or Desc. But in my case we need sorting in the order draft, assigned and cancel state. Based on applied in order by in python file

For example in the SQL the code -

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

The above sql code applied in the python

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

In the above query sorting selection value working in the pg_admin but in the python code its shows below error

Invalid "order" specified. A valid "order" specification is a comma-separated
list of valid field names (optionally followed by asc/desc for the direction)

Based on this sorting order by selection value how to apply in OpenERP? Override search method also applied the same sql query but shows same issue.

like image 810
ankita Avatar asked Nov 02 '22 16:11

ankita


1 Answers

Try creating a functional field with store attribute,which loads the function when state changes. for example

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"
like image 66
OmaL Avatar answered Nov 15 '22 06:11

OmaL