我不建议您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)
)
考虑到这种搜索方式比将值存储在数据库中效率低,因为您总是必须遍历所有记录来计算值。
顺便说一句,对于您的特定使用情况,您可以做的最好的事情是将字段名称创建为普通字符,这不是计算的。您可以设置默认值来创建名称。像这样,值将被存储,问题就会消失。