Odoo Help


How can I pass context to a one2many field?

Stefan Reisich
on 3/28/13, 10:00 AM 18,769 views

I have the following code:

class product_product(osv.osv):
    _name = 'product.product'
    _inherit = 'product.product'

    def test(self, cr, uid, ids, field_names=None, arg=None, context=None):
        result = {}
        if not ids: return result

        context['only_with_stock'] = True

        for id in ids:
            context['product_id'] = id
            location_obj = self.pool.get('stock.location')
            result[id] = location_obj.search(cr, uid, [('usage', '=', 'internal')], context=context)

        return result

    _columns = {
        'test': fields.function(test, type='one2many', relation='stock.location', string='Stock by Location'),




            context="{'product_id': active_id, 'only_with_stock': True}"
            name="Stock by Location"

        <record id="nfx_view_normal_procurement_locations_form" model="ir.ui.view">
            <field name="name">nfx_product.normal.procurement.locations.inherit</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="stock.view_normal_procurement_locations_form"/>
            <field name="arch" type="xml">
                <group name="lot" position="before" version="7.0">
                    <group string="Locations" attrs="{'invisible': [('type', '=', 'service')]}" groups="base.group_user">
                        <field name="test" nolabel="1" context="{'product_id': active_id, 'only_with_stock': True}">
                            <tree string="Stock Location">
                                <field name="complete_name"/>
                                <field name="stock_real"/>
                                <field name="stock_virtual" invisible="'product_id' not in context"/>



The test() function works well. It creates a one2many field with the stock locations. The complete_name field in the treeview is ok but the stock_real and stock_virtual fields are empty(0.00). I think because this fields calls _product_value() from stock/stock.py and _product_value() needs the product_id passed in the context. But it is not passed.

image description

What I'm doing wrong?


Stefan Reisich

--Stefan Reisich--
| 7 7 8
Pfungstadt, Germany
--Stefan Reisich--
Stefan Reisich
On 4/5/13, 11:21 AM

I have found the solution. The right way is indeed:

<field name="test" nolabel="1" context="{'product_id': active_id, 'only_with_stock': True}">

but there is a bug in OpenERP 7. The context is not passed. The bug is fixed and needs to be released. You can see and follow the fix here:


On this page you can see the modifications and fix it youself until the oficial fix is released.

I have tried to download this branch and use it as my web folder, updated all the modules of my database, but it doesn't resolve the problem... Am I supposed to install all of OpenERP once more ?

on 9/5/14, 12:55 PM
Mohamed Magdy
On 6/2/14, 2:17 AM

I wish this may help you:

    <field name="test" nolabel="1" context="{'default_product_id': active_id, 'default_only_with_stock': True}">


This worked like a charm. adding default_ before the field name eg default_product_id instead of product_id. Same can also be used to pass context in "create and edit" pop up as seen in the hr_expense.view_expenses_form view where groups_ref fields are set by context. if you try to create a new user from the expense form, some access right groups are automatically set.

Cyrus Waithaka
on 11/21/15, 6:28 PM
On 4/2/13, 7:33 AM

Pls. try with line <field name="test" nolabel="1" context="{'product_id': active_id, 'only_with_stock': True}" options="{"always_reload": True}">

sorry, no difference...

Stefan Reisich
on 4/2/13, 7:39 AM

Andreas Brueckl

--Andreas Brueckl--
| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 3/29/13, 4:21 AM

With the line

<field name="test" nolabel="1" context="{'product_id': id, 'only_with_stock': True}">

you try to set the "product_id" to id. In this case the field id must be part of your view. Try to add an invisible field with the id:

<field name="id" invisible="1"/>

Hi, you can use active_id instead of id, with active_id, no need to add a field id invisible. Bye

Cyril Gaspard (GEM)
on 3/29/13, 10:05 AM

both not working... :-(

Stefan Reisich
on 3/30/13, 2:54 PM

I thought id was right because id is given in the URL...

Stefan Reisich
on 4/2/13, 4:31 AM

Strange, the view is correct, since I saw this also in the account-addon. The problem must be in the function. You should remove method=True from the definition since this was only for v6.0. what is the value of the context within your function?

Andreas Brueckl
on 4/2/13, 6:05 AM

my function test is working well. It has the product_id in the context. The problem is the context is not passed to the fields <field name="stock_real"/> and <field name="stock_virtual"/>.

Stefan Reisich
on 4/2/13, 6:18 AM

try to remove method=True. Please update your question, because there you are using the field "test" and not "stock_real"

Andreas Brueckl
on 4/2/13, 6:21 AM

I'm sorry, I have tested it again. My function test don't get's the product_id passed in the context too. As you can see above the product_id is added to the context from ids. So the search in my function test is working well but because the product_id is not passed from <field name="test" nolabel="1" context="{'product_id': id( or active_id), 'only_with_stock': True}">, the sub fields <field name="stock_real"/> and <field name="stock_virtual"/> don't has the product_id in their context...

Stefan Reisich
on 4/2/13, 6:33 AM

method=True removed. Has no effect too... :-(

Stefan Reisich
on 4/2/13, 6:38 AM

Ok. I would just try <field name="stock_real" context="{'product_id': parent.id}"/>

Andreas Brueckl
on 4/2/13, 6:39 AM

no, don't work too. I even have tried <field name="stock_real" context="{'product_id': 49}"/> with no effect...

Stefan Reisich
on 4/2/13, 6:46 AM

Then I do not have any ideas. Maybe it is not possible in that special case where the fields of one2many objects are also function fields which require the context

Andreas Brueckl
on 4/2/13, 6:56 AM

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

2 follower(s)


Asked: 3/28/13, 10:00 AM
Seen: 18769 times
Last updated: 5/20/16, 9:37 AM