【零散技术】Odoo数据集排序(sorted)

【零散技术】Odoo数据集排序(sorted)

首页/资讯/【零散技术】Odoo数据集排序(sorted)

Odoo / ERP / 开发

序言:时间是我们最宝贵的财富,珍惜手上的每个时分

最近在写web接口,遇到不少数据排序问题。

例如:将某个数据集按时间排序,按规则排序。

如何快速的通过某个字段进行排序呢, odoo已经帮我们想好了:sorted。

此 sorted 非 python的 sorted/sort函数 切勿混为一谈。

    def sorted(self, key=None, reverse=False):
        """ Return the recordset ``self`` ordered by ``key``.

            :param key: either a function of one argument that returns a
                comparison key for each record, or a field name, or ``None``, in
                which case records are ordered according the default model's order

            :param reverse: if ``True``, return the result in reverse order
        """
        if key is None:
            recs = self.search([('id', 'in', self.ids)])
            return self.browse(reversed(recs._ids)) if reverse else recs
        if isinstance(key, pycompat.string_types):
            key = itemgetter(key)
        return self.browse(item.id for item in sorted(self, key=key, reverse=reverse))

使用方法(例):

对所有的产品 按sequence字段 顺序排列

 products = request.env['product.product'].sudo().search([])
 products = products.sorted('sequence', reverse=False)

承接odoo二次开发.部署实施.

技术交流 商务合作请添加

QQ:543127346 (穆尘)

微信: _ro_cd

访问我们的官网了解更多产品:https://www.ro-cd.com

联系咨询

通过微信、电话或邮箱,与我们沟通业务需求。

微信扫码咨询 · 点击图片查看原图,长按识别二维码

电话
19938056440028-83327012
邮箱
info@cd-ro.com