Community: Framework mailing list archives

expert-framework@mail.odoo.com

Re: Relational Division

by
Graeme Gellatly
- 06/28/2014 19:53:50
Sorry, I just can't understand why adding an ORM search operator is the right solution here given the difficulty of the underlying SQL and how simple it is to do in python/using ORM.  Especially since this is hardly a major issue faced by lots of people.  Then again I am more functional than technical.  Why can't you just omit that part of the domain and test after using python's set operations?  All the below is untested pseudocode with no exception catching but should give the idea.  Again - not a techie, just how a functional person with a little bit of python would write it.  I wouldn't mind betting this would be faster as well.

e.g.
In your function
right_set_of_ids = [n,n,n,...]
some_list_of_ids = self.search(...args)
# If you only want True results as a list - dict version
some_list_of_ids = [record_id for record_id, value in self.x2many_issubset(cr, uid, some_list_of_ids, 'x2m_field_name', right_set_of_ids, context=context).iteritems() if value]
# If you only want True results as a list - tuple version
some_list_of_ids = [record_id for record_id, value in self.x2many_issubset(cr, uid, some_list_of_ids, 'x2m_field_name', right_set_of_ids, context=context) if value]


then have
dict version - as everyone loves dicts
def x2many_issubset(self, cr, uid, ids, field_name, right_ids, context=None):
    res = {}
    right_ids = set(right_ids)
    for record in self.browse(cr, uid, ids, context=context):
        left_ids = set([x2m.id for x2m in record[field_name]])
        res[record.id] = left_ids.issubset(right_ids)
    return res

tuple version
def x2many_issubset(self, cr, uid, ids, field_name, right_ids, context=None):
    res = []
    right_ids = set(right_ids)
    for record in self.browse(cr, uid, ids, context=context):
        left_ids = set([x2m.id for x2m in record[field_name]])
        res.append((record.id, left_ids.issubset(right_ids))
    return res

Then either assign the function at ORM level or within the object model itself

e.g
openerp.osv.orm.BaseModel.x2many_issubset = x2many_issubset
or just using normal _inherit(s) mechanism for object models.

extend it any way you want, you could add a top level function which takes an operator for example and dynamically calls the correct lower set operation, but that one function covers the use case as I understand.  Should be quick, but one-liner versions below which for large lists of ids should be quite a bit quicker, tuple version quickest.

dict version
v2.6
def x2many_issubset(self, cr, uid, ids, field_name, right_ids, context=None):
    right_ids = set(right_ids)
    return dict([(rec.id, set([x2m.id for x2m in rec[field_name]]).issubset(right_ids) for rec in self.browse(cr, uid, ids, context=context)])
v2.7
def x2many_issubset(self, cr, uid, ids, field_name, right_ids, context=None):
    right_ids = set(right_ids)
    return {(rec.id, set([x2m.id for x2m in rec[field_name]]).issubset(right_ids) for rec in self.browse(cr, uid, ids, context=context)}

tuple version
def x2many_issubset(self, cr, uid, ids, field_name, right_ids, context=None):
    right_ids = set(right_ids)
    return [(rec.id, set([x2m.id for x2m in rec[field_name]]).issubset(right_ids) for rec in self.browse(cr, uid, ids, context=context)]



2014-06-29 4:07 GMT+12:00 <david@elaleman.co>:
Hola Alejandro!

gracias para tu respuesta... Estamos aca en Colombia trabajando sobre una maquina de impuestos avanzada para solucionar el tema de manera definitiva que tenemos con el sistema de ipuestos actual...

Para eso necesitamos una division relacional sobre un campo many2many, operacion en la base de datos que no esta habilitada por el ORM de odoo. Entonces buscamos ideas, alternativas, reccomendaciones comom solucionar ese problema. Es pocop probable que seamos los unicos / primeros que encontraron esta debilidad, entonces ojala que ya podemos contar con experiencia en la communidad..

Feliz Vacaciones y un cordial saludo

David


----------------------
David Arnold B.A. HSG
Gerente


​El Alemán S.A.S, Carrera 13 # 93 - 40 P4, Bogotá D.C, Colombia


2014-06-28 11:01 GMT-05:00 Alejandro Bravo Talavante <alejandrobravot@hotmail.es>:
Hola.
Veo que eres de Colombia así que te contestaré en castellano. En estos momentos estoy de vacaciones y apenas me da internet para leer el correo y no muy bien, entiendo que quieres un voto positivo en el tema, verdad? el día 30 estaré operativo, ahí podré leer tranquilamente el tema. Espero tu respuesta.
Saludos.

Subject: Relational Division
From: david@elaleman.co
To: alejandrobravot@hotmail.es
Date: Fri, 27 Jun 2014 10:05:40 +0000


Hi

I might give a pointer to this forum question to be discussed within this expert team.

Best, David
--
David Arnold Sent by OpenERP S.A. using OpenERP. Access your messages and documents in Odoo
--
Alejandro Sent by OpenERP S.A. using OpenERP. Access your messages and documents in Odoo

--
David Arnold Sent by OpenERP S.A. using OpenERP. Access your messages and documents through our Customer Portal