【问题标题】:How can I search by a computed field in Odoo?如何在 Odoo 中按计算字段进行搜索?
【发布时间】:2016-11-26 14:19:11
【问题描述】:

我正在尝试通过 Odoo 9 中的计算字段进行搜索。但它返回所有记录作为结果。

class Version(models.Model):
    _name='product_cars_application.version'

    name = fields.Char(compute="_get_name", store=True)

    @api.one
    def _get_name(self):
        self.name = "%s %s %s (%s)" % (self.brand_id,
                                       self.model_id.name,
                                       self.vname,
                                       self.year_id)

我尝试过使用store=True 并没有它,但名称字段未存储在数据库中。我还尝试从数据库中删除列“名称”,然后我更新了模块,但它没有存储计算。计算字段在表单视图上工作正常,但名称未存储,搜索不工作。

那么,如何按字段名称进行搜索?

【问题讨论】:

    标签: search odoo odoo-9 odoo-view computed-field


    【解决方案1】:

    将此添加到您的代码中。

     def _name_search(self,name, args=None, operator='ilike', limit=100):
        if operator == 'like': 
           operator = 'ilike'
    
        versions=self.search([('name', operator, name)], limit=limit)
        return versions.name_get()
    
     name = fields.Char(compute=_get_name, store=True,search=_name_search)
    

    您可以在文档中查看详细信息。

    https://www.odoo.com/documentation/8.0/reference/orm.html

    “计算字段”部分适合您。

    希望它会起作用。让我知道。

    【讨论】:

    • 必须做一些改变 def _name_search(self,name, args=None, operator='ilike', limit=100): if operator == 'like': operator = 'ilike' versions= self.search([('name', operator, name)], limit=limit) return versions.name_get()
    • 好的。我根据您的需要更改了代码。有同样问题的人可以更好地理解我们在这里做了什么。
    • 我不建议在计算字段中使用store=True,因为它们在某些情况下效果不佳。
    【解决方案2】:

    我不建议你在计算字段中使用 store=True 如果它们不是完全必要的(如果你想group by 这个字段是必需的),因为它们在某些情况下不能很好地工作,尽管最新版本工作得更好。所以,我认为你可以这样做:

    class Version(models.Model):
        _name = 'product_cars_application.version'
    
        name = fields.Char(
            compute="_compute_name",
            search="_search_name",
        )
    
        @api.one
        def _compute_name(self):
            self.name = "%s %s %s (%s)" % (self.brand_id,
                                        self.model_id.name,
                                        self.vname,
                                        self.year_id)
    
        def _search_name(self, operator, value):
            """ Actually this converts a domain into another one.
                With this new domain Odoo can search well
                A list of ids is the most easy way without computed fields
                    * [('id', 'in', id_list)]
            """
            if operator == 'ilike':
                name = self.env.context.get('name', False)
                if name is not False:
                    id_list = []
                    product_cars = self.env['product_cars_application.version'].search([])
                    for car in product_cars:
                        if name in car.name:
                            id_list.append(lot.id)
                    return [('id', 'in', lot_id_list)]
                else:
                    return [('id', 'in', [])]
            else:
                _logger.error(
                    'The field name is not searchable'
                    ' with the operator: {}',format(operator)
                )
    

    考虑到这种搜索方式比将值存储在数据库中效率更低,因为您总是需要遍历所有记录来计算值。

    顺便说一句,对于您的特定使用情况,您可以做的最好的事情是将字段名称创建为普通字符,这不是计算的。您可以设置默认值来创建名称。像这样,值将被存储,问题就会消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      相关资源
      最近更新 更多