将父销售订单中的项目填写到向导

Fill up items from parent sale order to wizard

我的目标是从父报价单中获取所有项目,并将其发送给向导 window。

我不知道我这样做对不对,但现在我可以从报价中获得所有产品,但不知道如何将它们填写到我的向导项目行中。

Quotation example

Wizard on that quotation

我删除了无关紧要的代码。

    from odoo import fields, models, api
    import logging

    class back_to_back_order(models.Model):
        _name = "back.to.back.order"
        _description = "Back to Back Order"

        line_ids =  fields.One2many('back.to.back.order.line','back_order_id', 'Order Lines', required=True)

    def get_items_from_quotation(self, context):
            items = []
            quotation_id = context['id']
            current_quotation = self.env['sale.order'].search([('id','=',quotation_id)])
            if quotation_id == current_quotation.id:
                    for line in current_quotation.order_line:
                            item = {
                                    'product_id': line.product_id,
                                    'qty': line.product_uom_qty,
                                    'price': line.price_unit,
                                    'subtotal': line.price_unit*line.product_uom_qty
                            }
                            items.append(item)


    class back_to_back_order_line(models.Model):
        _name = "back.to.back.order.line"
        _description = "Back to Back Order"


        product_id = fields.Many2one('product.product', 'Product')
        back_order_id = fields.Many2one('back.to.back.order', 'Back Order')
        qty = fields.Float('Quantity')
        price = fields.Float('Unit Price')
        subtotal = fields.Float('Subtotal')

首先,如果您正在创建一个向导,那么很可能您应该使用 models.TransientModel 而不是 model.Model 作为您的 类。

class back_to_back_order(models.TransientModel):
    ...

Wizard records are not meant to be persistent; they are automatically deleted from the database after a certain time. This is why they are called transient.


您提到您已经能够在您的向导中获取销售订单行,但您不确定如何使用数据填充您的背靠背订单行。

One2many and Many2many use a special "commands" format to manipulate the set of records stored in/associated with the field.

我最初是在 but they are also covered in the Documentation 上找到这些命令的。


至于您的具体应用,您应该能够简单地 create 您的 back.to.back.order.line 记录,只要您提供 back_order_id.[=21=,它们就会被链接起来]

@api.multi
def get_items_from_quotation(self, context):
    self.ensure_one()
    b2b_line_obj = self.env['back.to.back.order.line']
    quotation = self.env['sale.order'].browse(context['id'])
    if quotation:
        back_order_id = self.id
        for line in quotation.order_line:
            b2b_line_obj.create({
                'back_order_id': back_order_id,
                'product_id': line.product_id.id,
                'qty': line.product_uom_qty,
                'price': line.price_unit,
                'subtotal': line.price_unit * line.product_uom_qty,
            })