如何取消Odoo onchange上的One2many Field值

时间:2017-02-17 06:07:26

标签: python openerp odoo-9

  

我创建了一个模块,根据客户向PO订单行添加销售订单行。

Selecting customer

  

当我添加销售订单时,它将按订单顺序添加。

SO added

  

我试图取消特定ID与one2many字段的链接。在图片中添加SO字段

    class PurchaseOrder(models.Model):
    _inherit= "purchase.order"
    _name = "purchase.order"


    order_ids = fields.Many2many('sale.order', String="Add Order",domain="[('partner_id', 'child_of', partner_id),('state', 'in', ('quotation','socreated','done'))]")




    @api.onchange('order_ids')
    def orders_change(self):
        if not self.order_ids:
            return {}
        if not self.partner_customer_id:
            warning = {
                'title': _('Warning!'),
                'message': _('You must first select a partner!'),
            }
            # self.order_ids =False
            return {'warning': warning}

        line_ids = [] 

        u_ids=[]

        new_lines = self.env['purchase.order.line']


        for qt in self.order_ids:
            for i in self.order_line.mapped('sale_order_id'):

                line_ids.append(i)

            for u in self.order_ids:
                if u.id in line_id:
                    u_ids.append(u)
                    line_ids.remove(u)


            if line_ids and u_ids:
                lp = self.order_line.filtered(lambda r: r.sale_order_id <= line_ids[0])
                lp2 = self.order_line.filtered(lambda r: r.sale_order_id <= u_ids[0])


                for line in self.order_line:
                    if line in lp:
                        # self.order_line = [(6, 0, lp2.ids)]

                        line.unlink()

                continue



            for line in qt.order_line:
                # Load a PO line only once
                if line in self.order_line.mapped('sale_order_line_id'):
                    continue


                #seller section
                seller = line.product_id._select_seller(
                    line.product_id,
                    partner_id=self.partner_id,
                    quantity=line.product_uom_qty,
                    date=self.date_order and self.date_order[:10],
                    uom_id=line.product_uom)

                price_unit = self.env['account.tax']._fix_tax_included_price(seller.price,
                                                                             line.product_id.supplier_taxes_id,
                                                                             line.tax_id) if seller else 0.0
                if price_unit and seller and self.currency_id and seller.currency_id != self.currency_id:
                    price_unit = seller.currency_id.compute(price_unit, self.currency_id)

                if seller and line.product_uom and seller.product_uom != line.product_uom:
                    price_unit = self.env['product.uom']._compute_price(seller.product_uom.id, price_unit,
                                                                        to_uom_id=line.product_uom.id)
                unit = price_unit

                qty = line.product_uom_qty
                if float_compare(qty, 0.0, precision_rounding=line.product_uom.rounding) <= 0:
                    qty = 0.0
                tax_id = line.tax_id or line.product_id.taxes_id

                data = {
                    'sale_order_line_id': line.id,
                    'name': line.name,
                    'sequence_number':line.sequence_number,
                    'product_id': line.product_id.id,
                    'product_qty': qty,
                    'product_uom': line.product_id.uom_po_id or line.product_id.uom_id,
                    'price_unit': unit,
                    'cpo_no' : line.order_id.cpo_number,
                    'cpo_product_qty': qty,
                    'cpo_product_uom': line.product_id.uom_id,
                    'cpo_price_unit': line.price_unit,
                    'discount': 0.0,
                    'date_planned':(datetime.today() + relativedelta(weeks=4)).strftime(DEFAULT_SERVER_DATETIME_FORMAT),

                }
                new_line = new_lines.new(data)
                new_line._set_additional_fields(self)
                new_lines += new_line
        if new_lines :
            self.order_line += new_lines



class PurchaseOrderLine(models.Model):
    _inherit= "purchase.order.line"
    _name = "purchase.order.line"

    sale_order_line_id = fields.Many2one('sale.order.line', 'Order Line', ondelete='set null', select=True
                                        )

    sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order',
                                   store=False)
  

当我删除order_ids时,我想取消关联order_line(po)

中的相关行

link_ids会在选中时保存order_ids,当从order_ids中删除ID时,它将从link_ids中删除。 删除时,u_ids将保留order_id的其余部分。

当我从order_ids中删除id时,我想取消相关行与order_line的链接

但我无法将其删除。

我有用户[6,0,ids]方法来替换值,它不会在创建状态下工作。 请帮帮我。

4 个答案:

答案 0 :(得分:1)

以下是删除one2many中记录的常用逻辑

@api.onchage('field_name')
def onchange_field_name(self):
  for line in self.one2many_field_records:
    if line.order_id == 'your_satisfying_condition':
        line.unlink()

这是删除订单行记录的常用方法

答案 1 :(得分:1)

对不起,我已经迟到了。

尝试使用[(5,0,0)]删除所有记录,然后添加ID。这对我有用,但唯一的问题是,如果其他字段触发onchange方法不是相同的many2many或one2many字段,它可以工作

但是在创建模式下,如果您没有找到任何解决方案,为什么在更改所选订单ID时不使用更新按钮,他需要在显示one2many字段之前进行验证,这样您就可以使用写总是因为当他验证选择时会发生创建。我试图说的是找到它的工作方式,并找到一种方法来使用它

答案 2 :(得分:0)

对于你曾经建立关系的领域: -

sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order',
                               store=False)

请尝试给出属性ondelete =&#39; cascade&#39;并将行更改为: -

sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order', ondelete ='cascade'
                               store=False)

如果你这样说,当相关的销售订单被删除时,带有关系记录的one2many条目也将被删除。这只是一个例子,您可以尝试这种方式。 感谢

答案 3 :(得分:0)

new_lines = self.env['purchase.order.line']
        for qt in self.order_ids:
            for i in self.order_line.mapped('sale_order_id'):
                line_id.append(i.id)
                line_ids.append(i)
            for u in self.order_ids:
                if u.id in line_id:
                    u_id.append(u.id)
                    line_id.remove(u.id)
                    u_ids.append(u)
                    line_ids.remove(u)
            k = self.order_line.mapped('sale_order_id')
            if line_id and line_ids and u_ids:
                lp = self.order_line.filtered(lambda r: r.sale_order_id <= line_ids[0])
                lp6 = self.order_line.filtered(lambda r: r.sale_order_id not in line_ids)
                lp2 = self.order_line.filtered(lambda r: r.sale_order_id in u_ids)

                for line in self.order_line:
                    if line in lp:
                        self.update({
                            'order_line': [(5, _, _)],
                        })
                for o in lp6:
                    data = {
                        'sale_order_line_id': o.sale_order_line_id.id,
                        'name': o.name,
                        'sequence_number': o.sequence_number,
                        'product_id': o.product_id.id,
                        'product_qty': o.product_qty,
                        'product_uom': o.product_uom,
                        'price_unit': o.price_unit,
                        'cpo_no': o.cpo_no,
                        'cpo_product_qty': o.cpo_product_qty,
                        'cpo_product_uom': o.cpo_product_uom,
                        'cpo_price_unit': o.cpo_price_unit,
                        # 'quote_ref':line.order_id.origin,
                        'discount': 0.0,
                        'date_planned': (datetime.today() + relativedelta(weeks=4)).strftime(
                            DEFAULT_SERVER_DATETIME_FORMAT),
                    }
                    for line in qt.order_line:
                                # Load a PO line only once
                        if line in self.order_line.mapped('sale_order_line_id'):
                            continue
                    new_line = new_lines.new(data)
                    new_line._set_additional_fields(self)
                    new_lines += new_line



                    new_line = False

                continue