You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 15中如何从产品模板获取产品名称及最小库存数量

解决Odoo获取产品名称及对应最小库存的问题

首先明确两个核心点:

  • 产品名称在product.template中已有内置字段name,无需重新定义,直接复用即可;
  • 补货规则中的最小库存存储在stock.warehouse.orderpoint模型的product_min_qty字段中,product.template与该模型为一对多关联(一个产品可对应多个仓库的补货规则)。

方案1:获取默认仓库的最小库存(单值场景)

如果仅需展示产品在默认仓库的最小库存,可通过计算字段实现:

from odoo import fields, models, api

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

    # 复用原产品名字段,可自定义显示名称
    product_name = fields.Char(related='name', string='产品名称', readonly=True)
    
    # 计算字段,自动拉取默认仓库的最小库存
    product_qty_min = fields.Float(string='最小库存数量', compute='_compute_product_qty_min')

    @api.depends('orderpoint_ids.product_min_qty')
    def _compute_product_qty_min(self):
        # 获取当前公司的默认仓库
        default_warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
        for product in self:
            # 筛选该产品对应默认仓库的补货规则
            target_orderpoint = product.orderpoint_ids.filtered(lambda op: op.warehouse_id == default_warehouse)
            product.product_qty_min = target_orderpoint.product_min_qty if target_orderpoint else 0.0

方案2:展示所有仓库的最小库存(多值场景)

如果需要查看产品在所有仓库的补货规则配置,可直接关联补货规则列表:

from odoo import fields, models

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

    product_name = fields.Char(related='name', string='产品名称', readonly=True)
    # 直接复用内置的补货规则关联字段,无需重新定义
    orderpoint_ids = fields.One2many('stock.warehouse.orderpoint', 'product_tmpl_id', string='各仓库补货规则')

视图层配置(可选)

若要在产品表单/列表中展示上述字段,需在XML视图中添加以下内容:

<record id="product_template_form_inherit_qty_min" model="ir.ui.view">
    <field name="name">product.template.form.inherit.qty.min</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//div[@name='inventory']" position="inside">
            <group string="最小库存信息">
                <field name="product_name"/>
                <field name="product_qty_min"/>
                <!-- 方案2需添加以下列表字段 -->
                <field name="orderpoint_ids" widget="one2many_list">
                    <tree>
                        <field name="warehouse_id"/>
                        <field name="product_min_qty"/>
                    </tree>
                </field>
            </group>
        </xpath>
    </field>
</record>

关键提示

  • orderpoint_ids是product.template与补货规则模型的内置关联字段,无需手动创建;
  • 若产品未配置任何补货规则,计算字段会返回0.0,可根据业务需求调整默认值;
  • 多仓库管理场景下,优先选择方案2,能清晰展示各仓库的独立最小库存配置。

内容的提问来源于stack exchange,提问作者Zoro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 05:40:27