Search code examples
odooodoo-8openerp-8

Population of a selection box base on the Other Selection Box


Good Day! I have a problem I am trying to populate a Selection box base on the selected data on the other selection box here is my code

.py

licensetype = fields.Many2one('hr.licensetype','License Type')
license = fields.Many2one('hr.license','License')

@api.one
@api.onchange('licensetype')
def getlicense(self):
    if len(self.licensetype) > 0:
        mdlLicense= self.env['hr.license'].search([('license_name', '=', int(self.licensetype[0]))])
        #raise Warning(mdlLicense.ids)
        self.license = mdlLicense.ids

but still it populate all license I want to populate the License based on the selected License type. This is in Odoo8


Solution

  • Domains

    A domain is a list of criteria, each criteria being a triple (either a list or a tuple) of (field_name, operator, value).

    Here,

    • field_name : It's string type and must be from the current model or any relational traversal field through the Many2one/One2many field using membership (.) dot operator.

      - operator : It's for comparing field's value with passed value. Valid operator list (>, >=, <, <=, =, !=, =?, ilike, like =like, =ilike, not like, not ilike, childs_of, in, not in)

    • value : It's for comparing with field's value.

    Multiple criteria can be joined with three logical operators. Logical AND, logical OR, logical NOT.

    Read more about domain

    You can easily achieve this by defining domain for that field, no need to write any extra code.

    Just put domain in your xml code.

    <field name="licensetype" />
    <field name="license" domain="[('licensetype','=',licensetype)]" />
    

    Note :

    Remember there must be relation between hr.license and hr.licensetype. licensetype must be Many2one in hr.license.

    It will give the same effect as you want.