跳至内容
菜单
此问题已终结
1 回复
2736 查看

Hello,


I have a Many2one field ( product.product ) and I want to filter it by a given location_id.

Basically when I select the field I want to see only the products with quantity > 0 from a specific location.


How can I achieve this on Odoo 13 CE ?

形象
丢弃
最佳答案

Hi,

Try the following code,

from odoo import models, fields

class CustomModel(models.Model):
    _name = 'custom.model'

    location_id = fields.Many2one('stock.location', string='Location', required=True)
   
    //define default function   
    def _get_product_domain(self):
        domain = [('qty_available', '>', 0)]
        if self.location_id:
            domain.append(('location_id', '=', self.location_id.id))
        return domain
   
    product_id = fields.Many2one('product.product', string='Product', domain=lambda self: self._get_product_domain())
   
   
    after applying this domain, if you need to add the search filter then  you can add the product_id in the filter 

Hope it helps

形象
丢弃
相关帖文 回复 查看 活动
2
4月 21
11736
2
7月 15
5176
0
3月 15
4630
1
1月 21
4403
3
9月 20
72370