Use instance in One2Many.get to re-use cache
review585007
This commit is contained in:
1 parent
cf0636e095
commit
290791e0df
1 file changed
+8
-31
@@ -86,7 +86,8 @@ class One2Many(Field):
|
||||
res = {}
|
||||
for i in ids:
|
||||
res[i] = []
|
||||
ids2 = []
|
||||
|
||||
targets = []
|
||||
for i in range(0, len(ids), Transaction().cursor.IN_MAX):
|
||||
sub_ids = ids[i:i + Transaction().cursor.IN_MAX]
|
||||
if field._type == 'reference':
|
||||
@@ -94,37 +95,13 @@ class One2Many(Field):
|
||||
clause = [(self.field, 'in', references)]
|
||||
else:
|
||||
clause = [(self.field, 'in', sub_ids)]
|
||||
ids2.append(map(int, Relation.search(clause, order=self.order)))
|
||||
targets.append(Relation.search(clause, order=self.order))
|
||||
targets = list(chain(*targets))
|
||||
|
||||
cache = Transaction().cursor.get_cache(Transaction().context)
|
||||
cache.setdefault(self.model_name, {})
|
||||
ids3 = []
|
||||
for i in chain(*ids2):
|
||||
if i in cache[self.model_name] \
|
||||
and self.field in cache[self.model_name][i]:
|
||||
if field._type == 'reference':
|
||||
_, id_ = cache[self.model_name][i][self.field].split(',')
|
||||
id_ = int(id_)
|
||||
else:
|
||||
id_ = cache[self.model_name][i][self.field]
|
||||
res[id_].append(i)
|
||||
else:
|
||||
ids3.append(i)
|
||||
|
||||
if ids3:
|
||||
for i in Relation.read(ids3, [self.field]):
|
||||
if field._type == 'reference':
|
||||
_, id_ = i[self.field].split(',')
|
||||
id_ = int(id_)
|
||||
else:
|
||||
id_ = i[self.field]
|
||||
res[id_].append(i['id'])
|
||||
|
||||
index_of_ids2 = dict((i, index)
|
||||
for index, i in enumerate(chain(*ids2)))
|
||||
for id_, val in res.iteritems():
|
||||
res[id_] = tuple(sorted(val, key=lambda x: index_of_ids2[x]))
|
||||
return res
|
||||
for target in targets:
|
||||
origin_id = getattr(target, self.field).id
|
||||
res[origin_id].append(target.id)
|
||||
return dict((key, tuple(value)) for key, value in res.iteritems())
|
||||
|
||||
def set(self, ids, model, name, values):
|
||||
'''
|
||||
|
||||
Reference in new issue
Block a user