Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
2694 Vizualizări

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 ?

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
2
apr. 21
11697
2
iul. 15
5153
0
mar. 15
4574
1
ian. 21
4368
3
sept. 20
72324