【问题标题】:How to get products available quantity for individual stock (Odoo11)如何获取单个库存的产品可用数量(Odoo11)
【发布时间】:2019-08-17 16:49:27
【问题描述】:

我正在使用 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】:

    您必须加载/浏览具有特殊上下文值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

    【讨论】:

    • 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 = self.env['product.template'].with_context(warehouse=self.main_location).search([], limit=1) self.product_quantity=products print(f'Product Quantity: {self.product_quantity}')
    • 请不要在 cmets 中发布这么多代码,它不可读。 self.main_location 是记录集,而不是 ID 或仓库名称。请将其更改为self.main_location.idsearch() 会将产品作为记录集(在本例中为一个产品)而不是数量返回!它将返回一个记录集,其中找到的产品的计算数量字段是在“记住”位置或仓库的情况下计算的。所以如果我记得字段名称正确,它应该是self.product_quantity=products.qty_available
    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2018-01-27
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多