diff --git a/openerp/addons/test_inherit/models.py b/openerp/addons/test_inherit/models.py index 466318e22f5..a6d5aa96e9c 100644 --- a/openerp/addons/test_inherit/models.py +++ b/openerp/addons/test_inherit/models.py @@ -38,6 +38,7 @@ class mother(models.Model): _inherit = 'test.inherit.mother' field_in_mother = fields.Char() + partner_id = fields.Many2one('res.partner') # extend the name field: make it required and change its default value name = fields.Char(required=True, default='Bar') @@ -71,4 +72,11 @@ class daughter(models.Model): # change the default value of an inherited field name = fields.Char(default='Baz') + +class res_partner(models.Model): + _inherit = 'res.partner' + + # define a one2many field based on the inherited field partner_id + daughter_ids = fields.One2many('test.inherit.daughter', 'partner_id') + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/addons/test_inherit/tests/test_inherit.py b/openerp/addons/test_inherit/tests/test_inherit.py index 8867629be61..cda98193473 100644 --- a/openerp/addons/test_inherit/tests/test_inherit.py +++ b/openerp/addons/test_inherit/tests/test_inherit.py @@ -65,5 +65,17 @@ class test_inherits(common.TransactionCase): self.assertEqual(mother._columns['state'].selection, [('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')]) + def test_50_search_one2many(self): + """ check search on one2many field based on inherited many2one field. """ + # create a daughter record attached to partner Demo + partner_demo = self.env.ref('base.partner_demo') + daughter = self.env['test.inherit.daughter'].create({'partner_id': partner_demo.id}) + self.assertEqual(daughter.partner_id, partner_demo) + self.assertIn(daughter, partner_demo.daughter_ids) + + # search the partner from the daughter record + partners = self.env['res.partner'].search([('daughter_ids', 'in', daughter.ids)]) + self.assertIn(partner_demo, partners) + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/osv/expression.py b/openerp/osv/expression.py index 7f962c260fa..dedd043ca90 100644 --- a/openerp/osv/expression.py +++ b/openerp/osv/expression.py @@ -138,6 +138,7 @@ import traceback import openerp.modules from . import fields +from .. import SUPERUSER_ID from ..models import MAGIC_COLUMNS, BaseModel import openerp.tools as tools @@ -941,11 +942,16 @@ class expression(object): call_null = False push(create_substitution_leaf(leaf, FALSE_LEAF, model)) else: - ids2 = select_from_where(cr, column._fields_id, comodel._table, 'id', ids2, operator) - if ids2: + # determine ids1 <-- column._fields_id --- ids2 + if comodel._fields[column._fields_id].store: + ids1 = select_from_where(cr, column._fields_id, comodel._table, 'id', ids2, operator) + else: + recs = comodel.browse(cr, SUPERUSER_ID, ids2, {'prefetch_fields': False}) + ids1 = recs.mapped(column._fields_id).ids + if ids1: call_null = False o2m_op = 'not in' if operator in NEGATIVE_TERM_OPERATORS else 'in' - push(create_substitution_leaf(leaf, ('id', o2m_op, ids2), model)) + push(create_substitution_leaf(leaf, ('id', o2m_op, ids1), model)) if call_null: o2m_op = 'in' if operator in NEGATIVE_TERM_OPERATORS else 'not in'