diff --git a/bin/osv/orm.py b/bin/osv/orm.py index d9f86c95417..a322196f802 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -403,7 +403,7 @@ class orm_template(object): """Override this method to do specific things when a view on the object is opened.""" pass - def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None): + def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False): raise NotImplementedError(_('The read_group method is not implemented on this object !')) def _field_create(self, cr, context=None): @@ -2207,7 +2207,7 @@ class orm(orm_template): _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count', 'exists'] __logger = logging.getLogger('orm') __schema = logging.getLogger('orm.schema') - def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None): + def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False): """ Get the list of records in list view grouped by the given ``groupby`` fields @@ -2220,6 +2220,9 @@ class orm(orm_template): :param offset: optional number of records to skip :param limit: optional max number of records to return :param context: context arguments, like lang, time zone + :param order: optional ``order by`` specification, for overriding the natural + sort ordering of the groups, see also :py:meth:`~osv.osv.osv.search` + (supported only for many2one fields currently) :return: list of dictionaries(one dictionary for each record) containing: * the values of fields grouped by the fields in ``groupby`` argument @@ -2291,7 +2294,7 @@ class orm(orm_template): alldata[r['id']] = r del r['id'] if groupby and fget[groupby]['type'] == 'many2one': - data_ids = self.search(cr, uid, [('id', 'in', alldata.keys())], order=groupby, context=context) + data_ids = self.search(cr, uid, [('id', 'in', alldata.keys())], order=orderby or groupby, context=context) # the IDS of the records that has groupby field value = False or '' # should be added too data_ids += filter(lambda x:x not in data_ids, alldata.keys()) @@ -2299,6 +2302,7 @@ class orm(orm_template): # restore order of the search as read() uses the default _order (this is only for groups, so the size of data_read shoud be small): data.sort(lambda x,y: cmp(data_ids.index(x['id']), data_ids.index(y['id']))) else: + #FIXME: handle orderby param for non-m2o too! data = self.read(cr, uid, alldata.keys(), groupby and [groupby] or ['id'], context=context) if groupby: data.sort(lambda x,y:cmp(x[groupby],y[groupby]))