首页 > 其他 > 详细

odoo 计算字段搜索

时间:2021-06-18 11:46:40      阅读:20      评论:0      收藏:0      [点我收藏+]

源码示例:

    virtual_available = fields.Float(
        ‘Forecast Quantity‘, compute=‘_compute_quantities‘, search=‘_search_virtual_available‘,
        digits=dp.get_precision(‘Product Unit of Measure‘),
        help="Forecast quantity (computed as Quantity On Hand "
             "- Outgoing + Incoming)\n"
             "In a context with a single Stock Location, this includes "
             "goods stored in this location, or any of its children.\n"
             "In a context with a single Warehouse, this includes "
             "goods stored in the Stock Location of this Warehouse, or any "
             "of its children.\n"
             "Otherwise, this includes goods stored in any Stock Location "
             "with ‘internal‘ type.")
    def _search_virtual_available(self, operator, value):
        # TDE FIXME: should probably clean the search methods
        return self._search_product_quantity(operator, value, ‘virtual_available‘)

    def _search_product_quantity(self, operator, value, field):
        # TDE FIXME: should probably clean the search methods
        # to prevent sql injections
        if field not in (‘qty_available‘, ‘virtual_available‘, ‘incoming_qty‘, ‘outgoing_qty‘):
            raise UserError(_(‘Invalid domain left operand %s‘) % field)
        if operator not in (‘<‘, ‘>‘, ‘=‘, ‘!=‘, ‘<=‘, ‘>=‘):
            raise UserError(_(‘Invalid domain operator %s‘) % operator)
        if not isinstance(value, (float, int)):
            raise UserError(_(‘Invalid domain right operand %s‘) % value)

        # TODO: Still optimization possible when searching virtual quantities
        ids = []
        # Order the search on `id` to prevent the default order on the product name which slows
        # down the search because of the join on the translation table to get the translated names.
        for product in self.with_context(prefetch_fields=False).search([], order=‘id‘):
            if OPERATORS[operator](product[field], value):
                ids.append(product.id)
        return [(‘id‘, ‘in‘, ids)]

官网:https://www.odoo.com/zh_CN/forum/help-1/calculated-fields-in-search-filter-possible-118501

odoo 计算字段搜索

原文:https://www.cnblogs.com/qianxunman/p/14898382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!