Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How does tuples represent recursive relationships type in OpenERP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    In OpenERP, Tuple represents recursive relationship of type and the tuple models is a browsable model which start loading relationships type from and use it param ttypes which is a list of relationship types and follow 'one2many' and 'many2many'.

    Recursive relationships return tuple list of previously followed relationship tuple - to avoid duplicates during recursion.

    Use this funciont show below

    def recursive_relation(self, cr, uid, model, ttypes, relation_fields=None, suffix=None, context=None):
    
            if relation_fields is None:
                relation_fields = []
            local_rel_fields = []
            models = [x[1].model for x in relation_fields]
            model_obj = self.pool.get('ir.model')
            model_osv = self.pool.get(model.model)
            for colinfo in model_osv._all_columns.itervalues():
                coldef = colinfo.column
                coltype = coldef._type
                relation_field = None
                if coltype in ttypes and colinfo.column._obj not in models:
                    relation_model_id = model_obj.search(cr, UID_ROOT, [('model','=',coldef._obj)])[0]
                    relation_model_browse = model_obj.browse(cr, UID_ROOT, relation_model_id, context=context)
                    relation_osv = self.pool.get(coldef._obj)
                    if coltype == 'one2many':
                        # don't record reverse path if it's not a real m2o (that happens, but rarely)
                        dest_model_ci = relation_osv._all_columns
                        reverse_rel = coldef._fields_id
                        if reverse_rel in dest_model_ci and dest_model_ci[reverse_rel].column._type == 'many2one':
                            relation_field = ('%s.%s'%(reverse_rel, suffix)) if suffix else reverse_rel
                    local_rel_fields.append((relation_field, relation_model_browse))
                    for parent in relation_osv._inherits:
                        if parent not in models:
                            parent_model = self.pool.get(parent)
                            parent_colinfos = parent_model._all_columns
                            parent_model_browse = model_obj.browse(cr, UID_ROOT,
                                                                   model_obj.search(cr, UID_ROOT, [('model','=',parent)]))[0]
                            if relation_field and coldef._fields_id in parent_colinfos:
                                # inverse relationship is available in the parent
                                local_rel_fields.append((relation_field, parent_model_browse))
                            else:
                                # TODO: can we setup a proper rule to restrict inherited models
                                # in case the parent does not contain the reverse m2o?
                                local_rel_fields.append((None, parent_model_browse))
                    if relation_model_id != model.id and coltype in ['one2many', 'many2many']:
                        local_rel_fields += self._get_recursive_relations(cr, uid, relation_model_browse,
                            [coltype], relation_fields + local_rel_fields, suffix=relation_field, context=context)
            return local_rel_fields
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: