如何获得个别库存的产品可用数量(Odoo11)

问题描述 投票:-1回答:1

我正在使用odoo11(python3)并开发一个自定义模块。

我想获得个别库房的产品数量(手工或预测)。

我只选择任何一个仓库并显示该仓库的产品数量。实际上,我的目标是两件事1.产品总可用数量2.产品可用数量基于位置这是我的代码

class Threshold(models.Model):
    _name="threshold.threshold"
    main_location=fields.Many2many("stock.warehouse", string="Main Location")
    product=fields.Many2many("product.template", string="Product")
    category=fields.Many2one("product.category", string="Category")
    attribute=fields.Many2many("product.attribute", string="Attribute")
    threshold_value=fields.Integer(string="Threshold Value")
    transfer_quantity=fields.Integer(string="Transfer Quantity")
    status_button=fields.Selection([('0','Active'),('1','Dissmiss')], default='0', index=True, string="Status")
    threshold_selection=fields.Selection([('0','Product'),('1','Category'),], default= '0', index=True, string="Threshold Selection")

product_quantity=fields.Integer(compute="_product_based_on_warehouse", store=True)

@api.depends('main_location','product_quantity')
def _product_based_on_warehouse(self):
    count_products = 0 
self.env['product.template'].with_context(warehouse=self.main_location).search([], limit=1)
self.product_quantity=products print(f'Product Quantity: {self.product_quantity}')

python python-3.x odoo odoo-11
1个回答
1
投票

您必须使用特殊上下文值warehouse加载/浏览产品,以获取所有,一个或多个仓库的所有数量值。

# load all products and use a warehouse id
products = self.env['product.product'].with_context(warehouse=1).search([])
# load one product and use a warehouse name
products = self.env['product.product'].with_context(warehouse="Main Warehouse").search([], limit=1)
# load product with ID 1 and use a list of warehouse IDs
products = self.env['product.product'].with_context(warehouse=[1,2]).browse([1])

您可以使用产品的4个数量字段来获取所需的数量值。还可以选择使用location作为上下文值,方法与warehouse相同。

如果你想知道它的来源,请查看方法_compute_quantities_compute_quantities_dict和最重要的一个_get_domain_locations

© www.soinside.com 2019 - 2024. All rights reserved.