首页 > 解决方案 > 想要根据位置获取产品可用数量(odoo 12)

问题描述

我需要获取每个仓库的可用产品数量,并将其作为产品视图列表中的一列进行预览。

谁能指导我以下代码有什么问题?

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

    warehouse1_quantity = fields.Float('RYD Qty', compute='product_qty_ warehouse1_check')

    @api.multi
    def product_qty_ warehouse1_check(self): 
    for record in self:
          product = self.env['product.product'].browse(PRODUCT_ID)
          warehouse1_quantity = product.with_context({'warehouse' : 'RYD Inventory'}).qty_available 
          record.warehouse1_quantity
    <record id="product_template_view_tree_inherit" model="ir.ui.view">
        <field name="name">product.template.view.tree.inherit</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_tree_view"/>
        <field name="arch" type="xml">
            <xpath expr="//field[last()]" position="after">
                <field name="warehouse1_quantity"/>
            </xpath>
        </field>
    </record>

标签: pythontreeviewodoo

解决方案


我认为您尚未将值分配给对象变量。

它应该是这样的:

record.warehouse1_quantity = product.with_context({'warehouse' : 'RYD Inventory'}).qty_available 

推荐阅读