Odoo - Can't sum two field in model.py

廉价感情. 提交于 2020-04-17 20:28:51

问题


Why I can't sum two field in my model file. Code on below which is not working

I was trying as you see in last section to use onchange but without success.

for example:

i need the field "amount_after_disc" = "total_price" / "product_uom_qty"

    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")

回答1:


If you are using below 10 odoo versions please mention the variables total_price, product_uom_qty in xml field declaration of amount_after_disc using on_change function

If you are using above 9 odoo version please use @api.onchange() decorator

Please try to write the 'onchange_field' function in the 'sr.sale.price.history' model. And try to call this function after create as below

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()


来源:https://stackoverflow.com/questions/60755181/odoo-cant-sum-two-field-in-model-py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!