0
votes

I created a model Version, add a relation to the product model like this:

class product_template(models.Model):
    _inherit = 'product.template'

    versions_ids = fields.Many2many('mymodel.version',string='Versions')

class sale_order(models.Model):
    _inherit = 'sale.order'

    version_filter = fields.Many2one('mymodel.version')

I add a field "version_filter to the sale.order view, and now want to filter product search by this relation, but only the ones being adding not the previous ones addded

Like this:

<record id="sale_filter_append" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='order_line']" position="before">
            <group colspan="4">
                <field name="version_filter"/>
            </group>
        </xpath>
        <xpath expr="//field[@name='product_id']" position="replace">
            <field name="product_id"
                   context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'uom':product_uom, 'company_id': parent.company_id}"
                   attrs="{'readonly': ['|', ('qty_invoiced', '&gt;', 0), ('procurement_ids', '!=', [])]}"
                   domain="[('version_filter', 'in', versions_ids)]"
            />
        </xpath>
    </field>
</record>

But the "product_id" field is never replace.

I would rather make a modal form as in "Search more" option in many2one fields... but if someone could help me filter by domain the order_line product_id search its more than ok

Thanks for reading!

1

1 Answers

1
votes

Problem is there with the xpath. You should try following.

If you want to add domain in sales order line -> product_id in form view and tree view both then the example is given below.

<xpath expr="//notebook/page[@string='Order Lines']/field[@name='order_line']/form/group[1]/group/field[@name='product_id']" position="attributes">                 
    <attribute name = "domain">Add your domain</attribute>
</xpath>

<xpath expr="//notebook/page[@string='Order Lines']/field[@name='order_line']/tree[@string='Sales Order Lines']/field[@name='product_id']" position="replace">
    <field name="product_id" context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'uom':product_uom, 'company_id': parent.company_id}"
        attrs="{'readonly': ['|', ('qty_invoiced', '&gt;', 0), ('procurement_ids', '!=', [])]}"
        domain="[('version_filter', 'in', versions_ids)]" />        
</xpath>