Odoo - 浮动字段不在树视图上显示值 [英] Odoo - float field not display value on tree view

查看:91
本文介绍了Odoo - 浮动字段不在树视图上显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中添加了自定义浮动字段,并使用该字段在树视图上显示

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

但实际上在表单内部,该字段计算出正确的值并且工作正常,但在树视图列中,它显示所有记录的值都是 0.00 .. 为什么?

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?

这是我的模型代码:

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

这里是 XML 视图代码:

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>

推荐答案

sum 值会为每条记录重新初始化,并在for循环之外赋值,除了最后一条记录在for循环之外,计算将不起作用树视图.这些方法是用一个记录集调用的,在表单视图中,它被一个只包含一条记录的记录集调用,这就是它在表单视图中而不是在树视图中工作的原因.

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.

depends 装饰器缺失,尝试添加.

The depends decorator is missing, try to add it.

示例:

@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

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

这篇关于Odoo - 浮动字段不在树视图上显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆