2
votes

I have defined product_id in the class sale_order_line as below:

class sale_order_line(osv.osv):
    _inherit = 'sale.order.line'

    def _get_product_ids(self):

        return [('sale_ok', '=', True), ('state', 'in', ['sellable', 'custom']), ('id', '=', )]

    _columns = {
        'product_id': fields.many2one('product.product', 'Product',
                                      domain=_get_product_ids,
                                      change_default=True),
    }

The form view of sale.order has the below snippet where product_id is shown:

 <field name="product_id"
context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}"
groups="base.group_user"
on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, False, product_uos_qty, False, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)"/>

Initially in Sale Orders(model: sale.order) I select pricelist_id field. And then I click on 'Add an item' in Order Lines section to add a sale order line. In the form view of sale.order.line, I need to only show the products in the product_id based on the pricelist_id I selected earlier.

In product.product class the pricelist_id is a "dummy" field. So I am not able to figure out how to add domain filter since it will always return null value.

Could you please help me how to apply hard filter on product_id many2one field to show only the products based on selected pricelist_id in parent class?

2

2 Answers

1
votes

So sorry, but I can't fully understand your issue. You need to add a hard domain to sale.order.line's product_id field, based on the pricelist_id value set in sale.order. I'll quote you:

I need to only show the products in the 'product_id' based on the pricelist_id I selected earlier

As far as I remember on Openerp 7 (and next versions) you have a product_id field just on the pricelist version items records: that means you have to go through this class relation product.pricelistproduct.pricelist.versionproduct.pricelist.item
taking into account that each pricelist may have different version and each version different items.

Am I right or did I get it wrong? It sounds a bit crazy to me managing all this mess :) (unless you create some function fields directly in product.pricelist). Can you explain it better?

Apart from that, it seems to me that you might manage hard domain for field declarations using lambda functions. This can give you the chance to build more complex domains. Give it a try:

class sale_order_line(osv.osv):
    _inherit = 'sale.order.line'

    def _get_product_ids(self, cr, uid, ids, context=None):
        return [(...), (...)]

    _columns = {
        'product_id': fields.many2one('product.product', 'Product',
             #domain=lambda self: self._get_product_ids(),
             domain=lambda self: self._get_product_ids(self._cr, self._uid, [], self._context),
             change_default=True
         ),                                        
    }
0
votes

In my understanding, you need dependent dropdown many2one. For example I have two many2one fields (campus_id and department_id), and we want to change the department on the basis of campus field. If you want to do this then below is code snippet:

1    @api.onchange('campus_id')
2    def _campus_onchange(self):
3        res = {}
4        res['domain']={'department_id':[('campus_id', '=', self.campus_id.id)]}
5        return res