【问题标题】:how to add ondelete parameter to many2many field?如何将 ondelete 参数添加到 many2many 字段?
【发布时间】:2019-07-24 16:17:02
【问题描述】:

我添加了与汽车型号相关的 many2many 字段 所有记录都显示在行中 我需要如果我从任何列中删除任何数据,主模型“汽车”不会发生任何事情

我添加了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' )

【问题讨论】:

  • 能否请您详细说明一下您的情况。
  • 我有一个名为 Item 的模型,我正在为 Car 模型分配一个字段 many2many,如果我将任何汽车添加到 many2many 行中的项目,其中列是汽车、模型和年然后我需要在不改变车型的情况下在线编辑

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


【解决方案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 )

希望您明白这一点,您可能会找到更好的解决方案,但您需要一个模型来存储您所做的更改 在项目视图上

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多