Refs #28459 -- Improved performance of select_related() when model is prefetched from its parent.

This commit is contained in:
Sergey Fedoseev 2017-08-02 11:47:49 +05:00 committed by Tim Graham
parent 773c7888d4
commit ba1d82df63
1 changed files with 4 additions and 15 deletions

View File

@ -1665,21 +1665,10 @@ class RelatedPopulator:
] ]
self.reorder_for_init = None self.reorder_for_init = None
else: else:
model_init_attnames = [ attname_indexes = {select[idx][0].target.attname: idx for idx in select_fields}
f.attname for f in klass_info['model']._meta.concrete_fields model_init_attnames = (f.attname for f in klass_info['model']._meta.concrete_fields)
] self.init_list = [attname for attname in model_init_attnames if attname in attname_indexes]
reorder_map = [] self.reorder_for_init = operator.itemgetter(*[attname_indexes[attname] for attname in self.init_list])
for idx in select_fields:
field = select[idx][0].target
init_pos = model_init_attnames.index(field.attname)
reorder_map.append((init_pos, field.attname, idx))
reorder_map.sort()
self.init_list = [v[1] for v in reorder_map]
pos_list = [row_pos for _, _, row_pos in reorder_map]
def reorder_for_init(row):
return [row[row_pos] for row_pos in pos_list]
self.reorder_for_init = reorder_for_init
self.model_cls = klass_info['model'] self.model_cls = klass_info['model']
self.pk_idx = self.init_list.index(self.model_cls._meta.pk.attname) self.pk_idx = self.init_list.index(self.model_cls._meta.pk.attname)