简体   繁体   English

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

[英]How to search by computed field in odoo?

I can't search by computed field 我无法通过计算字段进行搜索

I override the name field in product.template to be computed so I need to search by name about the product 我覆盖了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_search(self, operator, value):
    if operator == 'like':
        operator = 'ilike'
        print('I'am In')
    return [('name', operator, value)]

name = fields.Char(string="Name", required=False ,compute=compute_amount,search=pro_search)

By default, compute field does not store in the database, it store=False . 默认情况下,compute字段不存储在数据库中,它store=False If you want to search, then you have to store that value in your database, so just set store=True . 如果要搜索,则必须将该值存储在数据库中,因此只需设置store=True

name = fields.Char(string="Name", required=False ,compute=compute_amount,search=pro_search, store=True)

Computed fields cannot be searched by default, because there's no database value for them. 默认情况下无法搜索计算字段,因为它们没有数据库值。

If the result of your computed field always depends on a combination of other fields, you can use the @api.depends() decorator on the computed method and set the field as store=True . 如果计算字段的结果总是依赖于其他字段的组合,则可以在计算方法上使用@api.depends()装饰器,并将字段设置为store=True This is the easiest way to do it. 这是最简单的方法。

However, on some circumstances it just isn't enough because the computed field result depends on more things (ie the context (date, lang, etc.)). 但是,在某些情况下,它还不够,因为计算的字段结果取决于更多的东西(即上下文(日期,语言等))。 In such case, you have to add a search method as you are doing above in your question. 在这种情况下,您必须像在上面的问题中一样添加搜索方法。

However, the main problem in your code is that the returned domain is, again, based on the name field in this part of the code: 但是,代码中的主要问题是返回的域再次基于代码的这一部分中的name字段:

return [('name', operator, value)]

name = fields.Char(string="Name", required=False ,compute=compute_amount,search=pro_search)

This is wrong. 这是错的。 The name field is computed, you cannot search on it. 计算name字段,您无法搜索它。 You have to translate this domain into one that only searches on stored fields. 您必须将此域转换为仅搜索存储字段的域。

Like others already stated, you need to add store=True in the field declaration. 与其他已经说明的一样,您需要在字段声明中添加store=True However, if you are doing this on an existing database, all the records will have the default value or be empty. 但是,如果在现有数据库上执行此操作,则所有记录都将具有默认值或为空。 This is because th ORM is just adding a new column in the psql table but no inserting the values. 这是因为ORM只是在psql表中添加了一个新列但没有插入值。

You need to manually call compute_amount on all your records in order to populate the database. 您需要在所有记录上手动调用compute_amount以填充数据库。

Note that you are never supposed to change the fields declaration for use with an existing database if you want to be sure not to have problems. 请注意,如果您希望确保没有问题,则永远不应该更改字段声明以用于现有数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM