odoo12 record 和 model 的关系 二

一中分析关键在ids 。不需要把所有数据都给放入。

但是ids从哪里来的?

一般for ord in records 一般都是在search 语法之后出现的。找search 语法查看

    
    def search(self, args, offset=0, limit=None, order=None, count=False):     
        res = self._search(args, offset=offset, limit=limit, order=order, count=count)
        return res if count else self.browse(res)
    
     def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
       
        # 拼接语句
        query = self._where_calc(args)
        self._apply_ir_rules(query, 'read')
        order_by = self._generate_order_by(order, query)
        from_clause, where_clause, where_clause_params = query.get_sql()

        where_str = where_clause and (" WHERE %s" % where_clause) or ''
        #判断数量
        if count:
            # Ignore order, limit and offset when just counting, they don't make sense and could
            # hurt performance
            query_str = 'SELECT count(1) FROM ' + from_clause + where_str
            self._cr.execute(query_str, where_clause_params)
            res = self._cr.fetchone()
            return res[0]

        limit_str = limit and ' limit %d' % limit or ''
        offset_str = offset and ' offset %d' % offset or ''
        # 只返回 id。省空间
        query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str
        self._cr.execute(query_str, where_clause_params)
        # 返回 id数组
        res = self._cr.fetchall()
        #去重复id数组 if x not in seen and not seen.add(x)
        # 是双重判断 seen.add(x)返回bool
        def _uniquify_list(seq):
            seen = set()
            return [x for x in seq if x not in seen and not seen.add(x)]
        # [x[0] for x in res]  就是id数组 [1,2,3,4]
        return _uniquify_list([x[0] for x in res])

    
    
    def browse(self, arg=None, prefetch=None):
        ids = _normalize_ids(arg)
        #assert all(isinstance(id, IdType) for id in ids), "Browsing invalid ids: %s" % ids
        return self._browse(ids, self.env, prefetch)

1、search 搜索出来的为了节省空间全部为 id

2、通过 _browse 给 _ids赋值 id

3、循环的时候 根据 需要操作的 recode(id) 在操作

猜你喜欢

转载自blog.csdn.net/sr50611/article/details/103701596
今日推荐