码迷,mamicode.com
首页 > 其他好文 > 详细

odoo 计算字段搜索

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

标签:instance   search   ble   because   war   injection   href   contex   rod   

源码示例:

    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 计算字段搜索

标签:instance   search   ble   because   war   injection   href   contex   rod   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!