Manoj
Manoj

Reputation: 119

Use of domain in search of One2many field in List view in Odoo-9

i have One2many field product_attributes in product.template and value field in product.attributes.custom

class ProductTemplateCus(models.Model):
    _inherit = 'product.template'
    product_attributes = fields.One2many('product.attributes.custom','product_id')

class ProductAttributes(models.Model):
    _name = 'product.attributes.custom'
    value = fields.Char(string='Value')
    product_id = fields.Many2one('product.template',string='Product')

Product 1 product_attributes contains 2 values:

value= 'color: red'
value= 'others: red'

product 2 product_attributes contains 2 values:

value= 'color: white'
value= 'others: red'

I did like below in search xml:

<field 
name="product_attributes" string="Color"
filter_domain="['&amp;',('product_attributes.value','ilike','color'),('product_attributes.value','ilike',self)]"
/>

So if red is searched, only product 1 containing both color and red should be shown. But I am unable to get result. I am getting both products.

Is there any solution for this?

Upvotes: 0

Views: 1483

Answers (3)

Manoj
Manoj

Reputation: 119

For solving my problem I used search function to get product ids which have selected attribute name and value only and included that in arguments in search function as

args += [['id', 'in', productids]]

I don't know if it was a right approach to do it. But it solved my problem.

Upvotes: 0

Eivydas Vilčinskas
Eivydas Vilčinskas

Reputation: 116

There are a few types of domain for field in the search view:

  • domain - just like the domain on the field declaration in the python class, limits the records got from the database;
  • filter_domain - overrides the name_search method which would normally have only ('name', 'ilike', self).

In your case, I believe, you need the filter_domain.


Just a suggestion, you could add the : after the attribute name to differentiate between the attribute and its value, given that you use the same convention for all your attributes: ('product_attributes.value', 'ilike', 'color: ')

The default operator between domains is & and in this case can be omitted.

Upvotes: 1

simahawk
simahawk

Reputation: 2431

AFAIK search_domain means nothing here. You should use domain instead.

Upvotes: 1

Related Questions