我有一个具有字段many2many(invoice_ids
)的类,其域依赖于另一个字段(department_id
)。我创建了一个onchange方法,以便每次修改department_id
字段时都返回域。这可以正常工作,但是当我保存对象并尝试对其进行编辑时,该域不起作用,因为未执行onchange。我该如何修改我的类,以便在编辑对象而不更改字段department_id
时域代码也能工作?
class MyClass(models.Model):
_name = 'my.class'
department_id = fields.Many2one('hr.department', string="Department", required=True)
invoice_ids = fields.Many2many('account.invoice', 'my_class_invoice_rel', 'my_class_id', 'invoice_id', string="Invoices")
@api.onchange('department_id')
def onchange_department_id(self):
if self.department_id:
some_search_id = self.env['some.model'].search([('department_id', '=', self.department_id.id)])
return {'domain': {'invoice_ids': [('state', '=', 'open'), ('some_model_id', 'not in', some_search_id), ('partner_id.department_id', 'child_of', self.department_id.id)]}}
答案 0 :(得分:0)
@Dayana 除了onchange方法之外,您还需要遍历计算方法。否则,我将向您显示所需的确切方案。
class FleetVehicle(models.Model):
_inherit = 'fleet.vehicle'
emirate_id = fields.Many2one('res.country.state', string="Emirate", domain="[('country_id', '=','United Arab Emirates')]")
@api.onchange('emirate_id')
def onchange_emirate(self):
self.plate_type_id = False
if self.emirate_id:
return {'domain': {'plate_type_id': [('id', 'in', self.emirate_id.plate_type_ids.ids)]}}
else:
return {'domain': {'plate_type_id': [('id', '=', False)]}}
class CountryState(models.Model):
_description = "Country state"
_inherit = 'res.country.state'
plate_type_ids = fields.Many2many('vehicle.plate.type', string="Plate Types")
希望您能理解这一点。谢谢!
别忘了对此进行投票并在刻度线上打上勾号。