如何使用包含Odoo v10中目标字段字符串的某些字符的字符串搜索记录?

时间:2017-11-05 14:28:05

标签: odoo-10 odoo

我正在使用Odoo v10。扫描条形码时,字符串包含char字段值的某些字符。例如,

  • 字段值('tracknum')=“20171103”
  • 输入字符串“xxxxxx20171103”或“xxxx20171103yyy”
  • 搜索字段

有什么办法吗? 我修改了搜索视图:

<field name="tracknum" string="Tracknum" filter_domain="..."/>

enter image description here

如何挖掘相关记录?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以像这样创建一个辅助计算字段

custom_name = fields.Char(
    string='Custom',
    compute='_compute_custom_name',
    search='_search_custom_name'
)

@api.multi
@api.depends()
def _compute_custom_name(self):
    ''' The field has to be a computed field
        You do not need to do anything here
    '''
    pass    

def _search_custom_name(self, operator, value):
    ''' Actually this converts a domain into another one.
        With this new domain Odoo can search well

        Arguments:
            * operator: if you are searchig words it is going to be ilike
            * value:    the string ro search

        The method could return something like this
            * [('id', 'in', id_list)]
    '''

    all_records = self.search([])   # recordset with all the values of the current model
    ids = []
    if operator == 'ilike':            
        ids = all_records.filtered(lambda r: r.tracknum in value).mapped('id')


    return [('id', 'in', ids)]

然后您可以将此字段添加到搜索视图中,如下所示:

<field name="custom_name" string="Tracking Number" />

请记住,它不是存储字段,因此效率非常低。每次要进行搜索时,都应迭代所有值。

将字段添加到搜索视图后,它应如下所示,跟踪编号应出现在字段名称中

SoapUi log