Odoo无法对model.py中的两个字段求和

2024-04-27 21:15:44 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么我不能在模型文件中对两个字段求和。下面的代码不起作用

正如您在上一节中看到的,我试图使用onchange,但没有成功

例如:

我需要字段“盘后金额”=“总价”/“产品数量”

    class ProductTemplate(models.Model):
    _inherit = 'product.template'

        sale_order_line_ids = self.env['sale.order.line'].sudo().search(domain,limit=sale_order_line_record_limit,order ='create_date desc')
        for line in sale_order_line_ids:
            sale_price_history_id = sale_history_obj.create({
                    'name':line.id,
                    'partner_id' : line.order_partner_id.id,
                    'user_id' : line.salesman_id.id,
                    'product_tmpl_id' : line.product_id.product_tmpl_id.id,
                    'variant_id' : line.product_id.id,
                    'sale_order_id' : line.order_id.id,
                    'sale_order_date' : line.order_id.date_order,
                    'product_uom_qty' : line.product_uom_qty,
                    'unit_price' : line.price_unit,
                    'currency_id' : line.currency_id.id,
                    'total_price' : line.price_subtotal
                })

@api.onchange('total_price', 'product_uom_qty')
   def onchange_field(self):
        if self.total_price or self.product_uom_qty:
            self.amount_after_disc = self.total_price / self.product_uom_qty

            sale_history_ids.append(sale_price_history_id.id)
        self.sale_price_history_ids = sale_history_ids

sale_price_history_ids = fields.Many2many("sr.sale.price.history",string="Sale Price History",compute="_get_sale_price_history")
class srSalePriceHistory(models.Model):
    _name = 'sr.sale.price.history'
    _description = 'Sale Price History'

    name = fields.Many2one("sale.order.line",string="Sale Order Line")  
    partner_id = fields.Many2one("res.partner",string="Customer")
    user_id = fields.Many2one("res.users",string="Sales Person")
    product_tmpl_id = fields.Many2one("product.template",string="Template Id")
    variant_id = fields.Many2one("product.product",string="Product")
    sale_order_id = fields.Many2one("sale.order",string="Sale Order")
    sale_order_date = fields.Datetime(string="Order Date")
    product_uom_qty = fields.Float(string="Quantity")
    unit_price = fields.Float(string="Price")
    currency_id = fields.Many2one("res.currency",string="Currency Id")
    total_price = fields.Monetary(string="Total")
    amount_after_disc = fields.Float(string="After Disc")

Tags: selfididsfieldsstringlineordersale
1条回答
网友
1楼 · 发布于 2024-04-27 21:15:44

如果您使用的是10个以下的odoo版本,请在xml字段中提及变量“总价”、“产品数量”和“使用on-change功能后的金额声明”

如果您使用的是9以上的odoo版本,请使用@api.onchange()decorator

请尝试在“sr.sale.price.history”模型中编写“onchange\u字段”函数。 并尝试在创建后调用此函数,如下所示

sale_order_line_ids = self.env['sale.order.line'].sudo().search(domain,limit=sale_order_line_record_limit,order ='create_date desc')
    for line in sale_order_line_ids:
        sale_price_history_id = sale_history_obj.create({
                'name':line.id,
                'partner_id' : line.order_partner_id.id,
                'user_id' : line.salesman_id.id,
                'product_tmpl_id' : line.product_id.product_tmpl_id.id,
                'variant_id' : line.product_id.id,
                'sale_order_id' : line.order_id.id,
                'sale_order_date' : line.order_id.date_order,
                'product_uom_qty' : line.product_uom_qty,
                'unit_price' : line.price_unit,
                'currency_id' : line.currency_id.id,
                'total_price' : line.price_subtotal
            })
        sale_price_history_id.onchange_field()

相关问题 更多 >