odoo 中记录重复

问题描述 投票:0回答:1

这是我的方法:

def action_save(self):
       for line in self.budget_investment_request_edit_division_line_ids:
            if not line.is_save:
                line.write({
                    'budget_investment_id': self.budget_investment_id.id,
                    'department_id': line.department_id.id,
                    'request_content': line.request_content,
                    'budget_investment_request_edit_division_id': self.id,
                    'is_save': True
                })

当我创建一个记录并输入按钮并通过上述方法应用时,就可以了。但是当我开始创建第二条记录时,第一条记录重复了。我曾尝试调试第一个,效果很好,但是当我开始执行第二个时,它已经显示 2 条记录,其中一条是重复的。希望有人能帮助我度过这个难关。

p/s:我使用了 onchange 方法来显示在视图上,这里是:

@api.onchange('budget_investment_id')
    def _onchange_budget_investment_id(self):
        line_ids = self.env['hmv.budget.investment.division.request.edit.line'].search([('budget_investment_id', '=',
                                                                                  self.budget_investment_id.id)])
        for line in line_ids:
            self.budget_investment_request_edit_division_line_ids += self.budget_investment_request_edit_division_line_ids.new(
                {
                    'budget_investment_id': line.budget_investment_id.id,
                    'department_id': line.department_id.id,
                    'request_content': line.request_content,
                    'is_save': True
                })
odoo odoo-17
1个回答
0
投票

我想知道这是否是由于您使用“new()”函数引起的。

    def new(self, values={}, origin=None, ref=None):
        """ new([values], [origin], [ref]) -> record

        Return a new record instance attached to the current environment and
        initialized with the provided ``value``. The record is *not* created
        in database, it only exists in memory.

        One can pass an ``origin`` record, which is the actual record behind the
        result. It is retrieved as ``record._origin``. Two new records with the
        same origin record are considered equal.

        One can also pass a ``ref`` value to identify the record among other new
        records. The reference is encapsulated in the ``id`` of the record.
        """

您可以尝试以下操作吗?

    @api.onchange('budget_investment_id')
    def _onchange_budget_investment_id(self):
        line_ids = self.env[
            'hmv.budget.investment.division.request.edit.line'
        ].search([('budget_investment_id', '=', self.budget_investment_id.id)])
        for line in line_ids:
            self.budget_investment_request_edit_division_line_ids |= (
                self.budget_investment_request_edit_division_line_ids.create(
                    {
                        'budget_investment_id': line.budget_investment_id.id,
                        'department_id': line.department_id.id,
                        'request_content': line.request_content,
                        'is_save': True
                    }
                )
            )

希望我猜对了,对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.