我正在使用Odoo 10,我正在尝试像下一张图片一样修改车队模块
我们需要“导体”(驾驶员)字段是唯一的,我的意思是,车辆只能分配一个驾驶员并且驾驶员不能分配多个车辆。
我使用了这段代码:
@api.constrains('driver_id')
def _check_driver(self):
for record in self:
if record.driver_id in self:
raise ValidationError("Driver already has a vehicle assigned")
但该代码不起作用。我该如何解决?
答案 0 :(得分:1)
您可以在字段定义中应用域过滤器,如下所示
#definition on the fleet.vehicle model
driver_id = fields.Many2one('res.partner', index=True, required=True,
ondelete='cascade', domain=[('vehicle_id', '=', False)])
#definition on the res.partner model
vehicle_id = fields.One2many('fleet.vehicle', 'driver_id')
或如下:
@api.constrains('driver_id')
def _check_driver(self):
FleetVehicle = self.env['fleet.vehicle']
for record in self:
if record.driver_id in self:
vehicle_count = FleetVehicle.search_count(['driver_id', '=', record.driver_id])
if vehicle_count > 0:
raise ValidationError("Driver already has a vehicle assigned")
答案 1 :(得分:0)
_sql_constraints = [
('driver_unique', 'UNIQUE(driver_id, )',
'The Driver has been already assigned to a vehicle !'),
]