我正在使用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}')发布于 2019-03-27 08:32:29
您必须使用一个特殊的上下文值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个quantity字段来获取所需的数量值。还可以选择使用location作为上下文值,其方式与使用warehouse的方式相同。
如果您想知道这是从哪里来的,请查看_compute_quantities、_compute_quantities_dict和最重要的方法_get_domain_locations。
https://stackoverflow.com/questions/55371796
复制相似问题