这是代码:
class Car(models.Model):
_name = 'car'
parking_id = fields.Many2one('cars')
class Parking(models.Model):
_name = 'parking'
cars_ids = fields.One2many('cars', 'parking_id')
class Group(models.Model):
_name = 'group'
parking_id = fields.Many2one('parking')
cars_ids = fields.Many2many('cars', lambda self: [('parking_id', '=', self.parking_id)])
我想要的是限制汽车的记录,当我通过停放在那辆汽车上以团体形式挑选它时。 但我的代码不起作用。怎么了?
答案 0 :(得分:0)
看来你的语法对于域名来说是错误的,如下所示:
class Group(models.Model):
_name = 'group'
parking_id = fields.Many2one('parking')
cars_ids = fields.Many2many('cars', domain=[('parking_id', '=', parking_id)])
答案 1 :(得分:0)
如果您在python代码中定义域,则在用户之后不会更改
选择每次用户更新域名所需的parking_id
更改parking_id
:
class Group(models.Model):
_name = 'group'
parking_id = fields.Many2one('parking')
# it's always better to define m2m field with full properties
# cars_ids = fields.Many2many('cars','group_cars_rel', 'group_id', 'car_id', 'List of cars' )
cars_ids = fields.Many2many('cars')
@api.onchange('parking_id')
def onchange_parking(self):
"""change the domain when user change parking_id"""
# you may need to empty the many2many field if the user change the parking
# if not just remove this line
self.cars_ids = [(5, 0, 0)] # remove all record from many2many
if self.parking_id:
return {'domain': {'cars_ids': [('parking_id', '=', self.parking_id.id)]}}
else:
# remove domain
return {'domain': {'cars_ids': []}}