odoo中分组查询函数read_group

read_group函数

read_group中英文注释解释

    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        """
        Get the list of records in list view grouped by the given ``groupby`` fields

        :param domain: list specifying search criteria [['field_name', 'operator', 'value'], ...]
        :param list fields: list of fields present in the list view specified on the object
        :param list groupby: list of groupby descriptions by which the records will be grouped.  
                A groupby description is either a field (then it will be grouped by that field)
                or a string 'field:groupby_function'.  Right now, the only functions supported
                are 'day', 'week', 'month', 'quarter' or 'year', and they only make sense for 
                date/datetime fields.
        :param int offset: optional number of records to skip
        :param int limit: optional max number of records to return
        :param list orderby: 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)
        :param bool lazy: if true, the results are only grouped by the first groupby and the 
                remaining groupbys are put in the __context key.  If false, all the groupbys are
                done in one call.
        :return: list of dictionaries(one dictionary for each record) containing:

                    * the values of fields grouped by the fields in ``groupby`` argument
                    * __domain: list of tuples specifying the search criteria
                    * __context: dictionary with argument like ``groupby``
        :rtype: [{'field_name_1': value, ...]
        :raise AccessError: * if user has no read rights on the requested object
                            * if user tries to bypass access rules for read on the requested object
        """
              result = self._read_group_raw(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)

        groupby = [groupby] if isinstance(groupby, basestring) else list(OrderedSet(groupby))
        dt = [
            f for f in groupby
            if self._fields[f.split(':')[0]].type in ('date', 'datetime')
        ]

        # iterate on all results and replace the "full" date/datetime value
        # (range, label) by just the formatted label, in-place
        for group in result:
            for df in dt:
                # could group on a date(time) field which is empty in some
                # records, in which case as with m2o the _raw value will be
                # `False` instead of a (value, label) pair. In that case,
                # leave the `False` value alone
                if group.get(df):
                    group[df] = group[df][1]
        return result
  

获取按给定groupby字段分组的列表视图中的记录列表

:param domain:list指定搜索条件[[‘field_name’,‘operator’,‘value’],…]
:param list fields:对象上指定的列表视图中存在的字段列表
:param list groupby:将记录分组的groupby描述列表。
groupby描述是一个字段(然后它将按该字段分组)
或字符串’field:groupby_function’。现在,支持的唯一功能
是’天’,‘周’,‘月’,‘季’或’年’,它们只是有意义的
日期/日期时间字段。
:param int offset:要跳过的可选记录数
:param int limit:可选的最大返回记录数
:param list orderby:可选order by规范,for
重写自然排序顺序
组,另见:py:meth:~osv.osv.osv.search
(目前仅支持多个字段)
:param bool lazy:如果为true,则结果仅按第一个groupby和
剩余的groupbys放在__context键中。如果为false,则所有组都是
在一个电话中完成。
:return:字典列表(每个记录一个字典)包含:

                *由``groupby``参数中的字段分组的字段值
                * __domain:指定搜索条件的元组列表
                * __context:带有``groupby``之类的参数的字典
    :rtype:[{'field_name_1':value,...]
    :引发AccessError:*如果用户对请求的对象没有读取权限
                        *如果用户试图绕过访问规则以读取请求的对象

我们可以看到对日期中’天’,‘周’,‘月’,'季’或’年’可以进行分组


chart_data = self.env['task_stage_data_analysis'].read_group(domain=
[('project', '=', project_id)], 
                                                                  fields=['current_date'],      
                                                                   groupby='current_date:month')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值