Odoo domain can't filter Unit(s) and Hour(s) from domain

Viewed 256

I have many2one field and trying to filter units of measure by name:

product_uom = fields.Many2one('product.uom', 'Unit of Measure', required=True, domain="[('name', 'in', ['m', 'Hour(s)', 'mile(s)', 'Day(s)', 'unit(s)'])]")

xml field:

 <field name="product_uom"/>

The problem is that in product_uom dropdown I can't find Unit(s) and Hour(s). What can be wrong with these field names?

P.S. for example if I make simple sql like that in my PgAdmin select * from product_uom where name in ('Unit(s)', 'Hour(s)') everything is fine.


Solved:

Changed my code into:

@api.multi
def get_domain(self):
    lang = self.env.context.get('lang')
    domain_list = ['m', 'Hour(s)', 'mile(s)', 'Day(s)', 'Unit(s)']
    if lang and lang != 'en_US':
        result = self.env['ir.translation'].search_read([
                                                    ('lang', '=', lang), 
                                                    ('src', 'in', domain_list)])
        domain_list = [rec['value'] for rec in result]
    return [('name', 'in', domain_list)]
1 Answers

May be you can user search read to extract the translated value of units:

 product_uom = fields.Many2one('product.uom', 'Unit of Measure', required=True, 
   domain=lambda self: self.get_domain())



 @api.multi
 def get_demain(self):
    """get default domain for uom taking languange in considiration"""
    lang = self.env.context.get('lang')
    domain_list = ['m', 'Hour(s)', 'mile(s)', 'Day(s)', 'unit(s)'])]
    if lang and lang != 'en':
        # language is not english
        result = self.env['ir.translation'].search_read([
                                                        ('lang', '=', lang), 
                                                        ('src', 'in', domain_list),
                                                        ('name', '=', 'product.uom')], 
                                                ['value'])
        domain_list = [rec['value'] for rec in result]

    return [('name', 'in', domain_list)]

I didn't try the code but i think you can make it better than this hope this helps you. like may if there is no translation for this words in user language ....etc

Related