Odoo搜索和分组查询函数:search、name_search、search_count、search_read、read_group
- search(): 搜索视图中调用
- search_count(): 视图中计算记录数时调用
- name_search(): 模型记录在被关联、被搜索时调用
- search_read(): many2one点开搜索更多时调用
- read_group(): 搜索视图分组时调用
这里主要讲read_group()函数
read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True)
- domain:domain用于过滤记录。这将作为read_group方法的搜索条件。
- fields:这是一个使用分组获取的字段列表。注意这里传入的字段应在groupby参数中,除非你使用了一些聚合函数。且可以使用数据库标准函数组件:(如:sum、count、avg等)
- groupby:这个参数是记录分组的字段列表。它让你可以根据多个字段来分组记录。
- offset:这个参数用于分页。如果想要跳过一些记录,可以使用该参数。
- limit:这个参数用于分页,它表示页面显示记录的最大量。
- orderby: 这个参数可以重写自然排序顺序。(orderby=”字段名”,按照指定字段排序,倒序 “字段名 desc”)
- lazy:这个参数接收布尔值。默认值为True。如果该参数为True,结果会在groupby参数中仅通过第一个字段来进行分组。结果中你会在__context和__domain键中获取到剩余的groupby参数及domain。如果该参数值设为False,它会用groupby参数中的所有字段对数据进行分组。
:param domain: list specifying search criteria [['field_name', 'operator', 'value'], ...]
domain条件
:param list fields: list of fields present in the list view specified on the object.
Each element is either 'field' (field name, using the default aggregation),
or 'field:agg' (aggregate field with aggregation function 'agg'),
or 'name:agg(field)' (aggregate field with 'agg' and return it as 'name').
The possible aggregation functions are the ones provided by PostgreSQL
(https://www.postgresql.org/docs/current/static/functions-aggregate.html)
and 'count_distinct', with the expected meaning.
分组之后显示的字段
: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.
是否弃用懒加载:如果为真,则结果仅按第一个groupby和其余的组放在__context键中。如果为假,则所有组都是一次调用搞定。
相关链接:
Odoo模型的内置方法重写
https://www.cnblogs.com/ygj0930/p/10826222.html
版权声明:本文为weixin_44863237原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。