如何将ondelete参数添加到many2many字段?

时间:2019-07-24 16:17:02

标签: python-3.x orm odoo odoo-11 odoo-12

我增加了很多与汽车型号相关的字段 所有记录均以行显示 如果我从任何列中删除任何数据,我需要主模型“汽车”

没有任何反应

我添加了ondelete参数,它不起作用

class item(models.Model):
_rec_name = 'name'
_name = 'item'

related = fields.Many2many(comodel_name="cars", string="related products",required=False, on_delete='NO ACTION', )

class cars(models.Model):
_name = 'cars'

name = fields.Char( string="Car",translate=True, ondelete='NO ACTION')
int_ref = fields.Char(string="Internal reference", required=False,store=True ,ondelete='NO ACTION')
model = fields.One2many(comodel_name="models", inverse_name="car", string="Models", required=False,ondelete='NO ACTION' )
pro = fields.Char(string="profit mergin", required=False, type='int' ,ondelete='NO ACTION')

rang = fields.One2many(comodel_name="yearrange", string="Range", inverse_name="product_id", ondelete='NO ACTION')
start = fields.Char(string="", required=False, ondelete='NO ACTION')
end = fields.Char(string="", required=False, ondelete='NO ACTION')
ignore = fields.Char(string="Ignore", required=False,ondelete='NO ACTION' )

1 个答案:

答案 0 :(得分:1)

例如,如果删除表示更改字段值 像start字段,然后在汽车菜单中打开汽车,您会发现 原始值。

如果这就是您想要的,那么您做错了many2many字段将更改 原始记录,因为它就像是通往它的大门。您需要做的是 创建另一个行为就像关系表的模型。

       class itemCare(models.Model):
            _name = 'items.cars.rel'

            item_id = fields.Many2one.......
            card_id = fields.Many2one.........

            # and you need to repeat all car fields here too
            # I don't know if by doing _inherit = 'cars' will do the trick

            # remove ondelete from cars and here too
            name = fields.Char( string="Car",translate=True, ondelete='NO ACTION')
            int_ref = fields.Char(string="Internal reference", required=False,store=True ,ondelete='NO ACTION')
            model = fields.One2many(comodel_name="models", inverse_name="car", string="Models", required=False,ondelete='NO ACTION' )
            pro = fields.Char(string="profit mergin", required=False, type='int' ,ondelete='NO ACTION')

            rang = fields.One2many(comodel_name="yearrange", string="Range", inverse_name="product_id", ondelete='NO ACTION')
            start = fields.Char(string="", required=False, ondelete='NO ACTION')
            end = fields.Char(string="", required=False, ondelete='NO ACTION')
            ignore = fields.Char(string="Ignore", required=False,ondelete='NO ACTION' )


            @api.onchancge('card_id')
            def onchange_card(self):
                if self.card_id:
                    # fill up all your field with same value of card
                    self.name = self.card_id.name
                    .....
                    .....
                else:
                    # empty all field when we remove the card
                    self.name = False
                    .....
                    ....

        class item(models.Model):
            _rec_name = 'name'
            _name = 'item'

            # the relation now is one2many to the new model that holds the fields of the car
            related = fields.One2many(comodel_name="items.cars.rel", string="related products",required=False )

希望您有想法,您可能会找到一个更好的解决方案,因为您需要一个模型来存储所做的更改 在项目视图上