如何自动填充一个One2many

how to automatically fill a One2many

我目前正在使用 odoo [销售]-销售订单的本机模块,我想在按下将我重定向到我创建的另一个新公式的按钮时通过 One2many 字段的注册有一个字段 One2Mnay.

示例: 这是 odo 的原生模块,我在其中注册我的销售订单:

我创建了一个红色的新按钮 [registrar cotizacion], 调用方法:[open_budget] 允许打开我创建的新表单并有一个字段 one2many :

我的模特:

新形态模型:

class BudgetTwo(models.Model):
    _name = 'budget.two'
    name = fields.Char(string ='Nombre', copy=False, index=True ,default ="Nuevo")
    partner_id =fields.Many2one('res.partner' ,string ='Cliente', copy=False, index=True,required=True)
    deliver_date = fields.Date(string ='Fecha de Entrega')
    expiration_date = fields.Date(string ='Fecha de expiración')
    pay_place =fields.Many2one('account.payment.term' ,string='Plazo de Pago')
    order_line = fields.One2many('budget.table','budget_id' ,string = 'Pedidos' )
    total = fields.Float(string = 'Total:' ,compute="_total")
    btn_d = fields.Boolean(default = False)

继承odoo自带模块:

class InheritOrder(models.Model):
    _inherit = 'sale.order'
    @api.multi
    def open_budget(self):
        if self.order_line:
            for element in self.order_line:
                data = element.product_id.id
        else:print('none')
        print(data)

        return {
            'name': ('Payments'),
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'budget.two',
            'view_id': False,
            'type': 'ir.actions.act_window',
            'context': {
                'default_partner_id': self.partner_id.id,
                'default_order_line.product_id': data,
                'default_order_line.product_id': data,
            },
        }

odoo 的本机模块有一个字段 One2many 是订单,当我按下新的 botton loogre 重定向到我创建的新表单并创建了一个字典,它有一个名为 context 的键我可以传递记录我在字段中有但我在one2many值中的记录没有通过。

最后:

'context': {
                'default_partner_id': self.partner_id.id,
                'default_order_line.product_id': data,
                'default_order_line.product_id': data,
            },

desauld_pernert_id如果记录通过但是default_order_line.product_id没有通过的记录就是on2many,可以看到:

你可以这样试试

class InheritOrder(models.Model):
    _inherit = 'sale.order'

    @api.multi
    def open_budget(self):
        ctx = dict()
        if self.order_line:
           products={}
           for line in self.order_line:
               # You can get whatever field from sale order line
               products.update({'product_id': line.product_id.id})
               product_line.append((0, 0, products))

           ctx.update({
                'default_order_line': product_line,
                'default_partner_id': self.partner_id.id,
             })

        return {
            'name': ('Payments'),
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'budget.two',
            'view_id': False,
            'type': 'ir.actions.act_window',
            'context': ctx,
          }

有一种特殊的语法可以将值传递给 x2many fields

如果你想创建新行,你应该使用:

'context': {
            'default_partner_id': self.partner_id.id,
            'default_order_line.product_id': [(0, 0, {'field_name': its value,..}),
                                               ..., 
                                              (0, 0, {'field_name': its value, ..})
                                             ]
           },