I have created a new model named product.service.type. Then, in product.product model, I have also created a Many2many field (named service_type, pointing to product.service.type model).
Now I have the model test, which has product_id and service_type_id fields, both Many2one pointing to product.product and product.service.type respectively.
What I want is that if you select a product, the service type domain changes to show only the service types of the selected product. I managed this through an onchange:
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
if product_id:
product = self.pool.get('product.product').browse(
cr, uid, [product_id], context=context)
service_type_ids = product.service_type.mapped('id')
return {
'domain': {
'service_type_id': [('id', 'in', service_type_ids)],
},
}
This is working great, the problem is when you are editing the record (not creating a new one), because in this case the onchange is not being executed and therefore, the domain shows all service types.
You can see the same problem in partners form, with the field title. Create a new partner which is a company, the domain of title field changes to allow you select only records like Corp., Ltd., etc., but if you set that the partner is a contact, you can select among records like Doctor, Madam, Miss, etc. Now, save the partner with the data you want, and go to other menu of the topbar. Return to partners form and open the created partner to edit it. Check the title field without changing the is_company field. Now you have all titles available, despite your partner belongs to a specific class (company or contact).
How could I fix this problem?