如何自动填充一个One2many

时间:2018-08-27 13:00:56

标签: python odoo odoo-10

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

示例:     这是我注册销售订单的odo的本地模块: enter image description here

我创建了一个新的红色的[botistrar cotizacion] botton, 称为方法:[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的键的字典时,我可以传递我拥有的记录在字段中,但是我在one2许多值中的记录没有通过。

最后:

'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的记录,如您所见:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试这样

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,
          }

答案 1 :(得分:0)

有一种特殊的语法可将值传递给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, ..})
                                             ]
           },