Odoo - float field not display value on tree view

左心房为你撑大大i 提交于 2020-07-23 06:43:17

问题


I have added custom float field to form and I used that field to display on tree view

but actually inside the form the field is calculated the correct value and working correctly but in the tree view column it's showing 0.00 value for all records.. why?

here's my model code:

from odoo import models, fields, api

class PickingTotalQty(models.Model):
    _inherit = 'stock.picking'

    sum_dmd_qty = fields.Float(compute='calculate_dmd_qty', string='Total Demand Quantity')
    sum_reserved_qty = fields.Float(compute='calculate_reserved_qty', string='Total Reserved Quantity')
    reserved_qty_per = fields.Float(compute='calculate_reserved_per', string='Reserved (%)')

    def calculate_dmd_qty(self):
        for rs in self:
            dmdqty = 0
            for line in rs.move_lines:
                dmdqty += line.product_uom_qty
        rs.sum_dmd_qty = dmdqty

    def calculate_reserved_qty(self):
        for rs in self:
            reservedqty = 0
            for line in rs.move_lines:
                reservedqty += line.reserved_availability
        rs.sum_reserved_qty = reservedqty

    @api.depends('sum_reserved_qty', 'sum_dmd_qty')
    def calculate_reserved_per(self):
        for rec in self:
            if rec.sum_dmd_qty > 0:
                rec.reserved_qty_per = rec.sum_reserved_qty / rec.sum_dmd_qty

and here is XML view code:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="inherit_stock_picking_form_view" model="ir.ui.view">
            <field name="name">Stock Picking</field>
            <field name="model">stock.picking</field>
            <field name="inherit_id" ref="stock.view_picking_form"/>
            <field name="arch" type="xml">
                <xpath expr="/form/sheet/notebook" position="after">
                    <group>
                        <group>
                            <field name="reserved_qty_per" nolabel="0"/>
                        </group>
                    </group>
                </xpath>
            </field>
        </record>
        <record id="inherit_stock_picking_tree_view2" model="ir.ui.view">
            <field name="name">Stock Picking2</field>
            <field name="model">stock.picking</field>
            <field name="inherit_id" ref="stock.vpicktree"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='state']" position="after">
                      <field name="reserved_qty_per"/>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

回答1:


The sum value is reinitialized for each record and assigned outside the for loops, the calculation will not work except for the last record in the tree view. the methods are called with a recordset, in the form view it is called with a recordset containing only the one record, and this is why it works in the form view and not in the tree view.

To correct that issue move the last line of the two first methods inside the first loop.

The depends decorator is missing, try to add it.

Example:

@api.depends("move_lines.product_uom_qty")
def calculate_dmd_qty(self):
    for rs in self:
        dmdqty = 0
        for line in rs.move_ids_without_package:
            dmdqty += line.product_uom_qty
        rs.sum_dmd_qty = dmdqty

@api.depends("move_lines.reserved_availability")
def calculate_reserved_qty(self):
    for rs in self:
        reservedqty = 0
        for line in rs.move_ids_without_package:
            reservedqty += line.reserved_availability
        rs.sum_reserved_qty = reservedqty

Edit:

<odoo>
    <record id="view_picking_form" model="ir.ui.view">
        <field name="name">view.picking.form</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_form"/>
        <field name="arch" type="xml">
            <xpath expr='//notebook' position="after">
                <group>
                    <field name="reserved_qty_per"/>
                </group>
            </xpath>
        </field>
    </record>

    <record id="vpicktree" model="ir.ui.view">
        <field name="name">view.vpicktree</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.vpicktree"/>
        <field name="arch" type="xml">
            <field name="state" position="before">
                <field name="reserved_qty_per"/>
            </field>
        </field>
    </record>
</odoo>


来源:https://stackoverflow.com/questions/62852510/odoo-float-field-not-display-value-on-tree-view

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