2

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

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)

this is The Error from Log

raise ValueError("Invalid leaf %s" % str(self.leaf))
ValueError: Invalid leaf (False, 'ilike', 'تيل')
James R
  • 4,571
  • 3
  • 30
  • 45
Mohamed Fouad
  • 476
  • 9
  • 27

1 Answers1

2

Non persistent fields aren't searchable. That's because Odoo tries to parse domain tuples/filters into a SQL query which ofcourse is searching on existing columns. Non stored fields don't have a table column.

You have two options now:

  1. make the field stored with store=True
  2. define a search method and set it on the field with search='name_of_search_method (more information here)

The first option is simple, but storing computed fields can have a bad or good impact on performance. The second option is much more difficult, because you have to think how to form the search into using the dependent fields or every field you are using on computation.

CZoellner
  • 13,553
  • 3
  • 25
  • 38