This question has been flagged

Hello all,

I'm in a purchase order. I add a purchase order line to this purchase order.

When I begin to enter the product_id for my line, the name_search method (of product.product model) is triggered automatically.

I have overriden the name_search method, but I would need to add a new argument or a new context to this name_search method. I would need the partner_id of the current purchase order in my new code.

How to get the current purchase order partner_id in an overriden name_search method?

Python signature of name_search method on product.product :

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):


EDIT #1

For each product.product, I have now a field 'variant_seller_ids'. A product.product item can have many suppliers. For each supplier, I can define the product_code at this supplier. For example, if product.product item is purchased at AAA supplier, his product_code could be AAA0001. If the product.product item is purchased at BBB supplier, his product_code could be BBB00001. 

That's why I would need the purchase order partner_id in my overriden name_search method : Because I want to search the product.product by product_code in the right supplier. If a product.product has many suppliers defined, I just want to search the right product_code .


EDIT #2

When the name_search method is called from a sale order line, the context contains more things :

context :: {'lang': 'en_US', 'tz': 'America/Montreal', 'uid': 1, 'company_id': 1, 'pricelist': 1, 'partner_id': 78, 'uom': False, 'quantity': 1}

As you can see, the partner_id is there in this case. I try to find the place in the code where the 'partner_id' is added to context, but I don't find it... If I could find how the partner_id is added to the context in this case, may be I could imitate the code for purchase order lines.


Avatar
Discard

If you could have the partner_id inside the method then what is it that you're really trying to solve? I'm afraid this is some kind of "The XY Problem". http://xyproblem.info/

Author

Thank for your comment. See edit#1.

I have just came across a similar problem as yours and now it makes sense to have extra arguments in name_search method. Nice observation on Edit #2, and your solution below solves my problem. Thanks!

Author Best Answer

In a purchase order, when you added a new purchase order line, to have the partner_id of the purchase order in the context when the name_search method is trigerred, you just have to modify the context of the product_id field.

XML

<record id="purchase_order_form_inherited" model="ir.ui.view">
            <field name="name">purchase.order.form.inherited</field>
            <field name="model">purchase.order</field>
            <field name="inherit_id" ref="purchase.purchase_order_form" />
            <field name="arch" type="xml">
                <xpath expr="//field[@name='order_line']//field[@name='product_id']" position="attributes">
                    <attribute name="context">{'partner_id':parent.partner_id}</attribute>
                </xpath>

            </field>
        </record>



Avatar
Discard