Odoo Help


How to add new args or new context to overriden name_search method?

Pascal Tremblay
on 3/29/17, 12:28 PM 968 views

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):


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 .


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.

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/

Muhammad Irfan
on 3/29/17, 1:47 PM

Thank for your comment. See edit#1.

Pascal Tremblay
on 3/29/17, 2:04 PM

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!

Muhammad Irfan
on 4/17/17, 11:38 AM

Pascal Tremblay

--Pascal Tremblay--
| 5 3 7
Alma, Canada
--Pascal Tremblay--

Pascal Tremblay
On 3/30/17, 3:18 PM

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.


<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>


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 3/29/17, 12:28 PM
Seen: 968 times
Last updated: 3/30/17, 3:18 PM