This question has been flagged
1 Reply
3062 Views

i'm searching the records from another model and after getting the record i'm writing that record in lines using one2many relation 

here is my code in which i'm writing the the data in one2many line 

for file in files:
self.allotment_line_id = [(0, 0, {'member_id': file.membership_id.id,

'file_id': file.id,

'tracking_no': file.tracking_id,

'sector_id': file.sector_id.id,

'category_id': file.category_id.id,

'unit_category_type_id': file.unit_category_type_id.id,

'size_id': file.size_id.id,

'street_id': file.street_id.id,

'preference_ids': [(6, 0, file.preference_ids.mapped('factor_id.id'))]
  })] 
after this i'm using on change api for triggering the my inventory_id field for applying the domain HERE IS THE CODE FOR DOMAIN

@api.onchange('inventory_id') 
def _onchange_inventory(self): 
    res = {'domain': { 'inventory_id': [('state', '=', 'available_for_sale'), 
    ('category_id', '=', self.category_id.id), 
    ('unit_category_type_id', '=',self.unit_category_type_id.id)] 
    }
    }

now in lines category_id and unit_category_type_id.id have values, and when i click the inventory_id field it will show me complete inventory and then i select one of them after that if i click again on inventory_id field then it trigger the inventory_id and after that it shows me 

my required result related to domain 
but i want that on first time clicking on inventory_id field it trigger my function 

Avatar
Discard
Best Answer

Hi,The onchange function only triggers after the field has changed. Here you want to return the domain for inventory_id. In your snippet I found some fields values are used in your function. So it means before selecting inventory_id you set those fields values. So you can include the fields in @api.onchange() like this:

@api.onchange('category_id', 'unit_category_type_id')
def _onchange_inventory(self):
res = {'domain': {
'inventory_id': [('state', '=', 'available_for_sale'), ('category_id', '=', self.category_id.id),
('unit_category_type_id', '=', self.unit_category_type_id.id)]}}
return res

Regards

Avatar
Discard