如何通过odoo计算字段进行搜索?

问题描述 投票:2回答:1

我无法通过计算字段进行搜索

我覆盖了product.template中的name字段,因此我需要按名称搜索产品

class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.depends('item', 'car', 'model', 'dsc', 'drc', 'year', 'org')
def compute_amount(self):
    for rec in self:
        rec.name = " ".join(
            [rec.item and rec.item.name or "", rec.car and rec.car.name or "", rec.model and rec.model.name or "",
             rec.dsc and rec.dsc.name or "", rec.drc and rec.drc.name or "", rec.org and rec.org.name or "",
             rec.year and rec.year.name or ""])

def pro_searach(self, operator, value):

    if operator == 'like':
        operator = 'ilike'
        name = self.env['product.template'].search([('name', operator, value)], limit=None)
    return [(self.name, operator, value)]
name = fields.Char(string="Name", required=False ,compute=compute_amount,search=pro_searach)

这是日志中的错误

raise ValueError("Invalid leaf %s" % str(self.leaf))
ValueError: Invalid leaf (False, 'ilike', 'تيل')
python python-3.x odoo odoo-11
1个回答
1
投票

非持久字段不可搜索。这是因为Odoo试图将域元组/过滤器解析为SQL查询,而该查询当前正在现有列上进行搜索。非存储字段没有表列。

你现在有两个选择:

  1. 使用store=True存储该字段
  2. 定义一个搜索方法并使用search='name_of_search_methodmore information here)在字段上设置它

第一个选项很简单,但存储计算字段可能会对性能产生不良或良好的影响。第二个选项要困难得多,因为您必须考虑如何使用依赖字段或您在计算时使用的每个字段进行搜索。

© www.soinside.com 2019 - 2024. All rights reserved.