further optimization

This commit is contained in:
collerek
2020-11-25 21:14:37 +01:00
parent d6f995d349
commit 2fd2bd9e3f

View File

@ -150,30 +150,15 @@ class PrefetchQuery:
return [] return []
@staticmethod @staticmethod
def _get_model_id_and_field_name( def _get_model_id(target_field: Type["BaseField"], model: "Model") -> Optional[int]:
target_field: Type["BaseField"], model: "Model" if target_field.virtual or issubclass(target_field, ManyToManyField):
) -> Tuple[bool, Optional[str], Optional[int]]: return model.pk
if target_field.virtual: related_name = model.resolve_relation_name(model, target_field.to)
is_multi = False related_model = getattr(model, related_name)
field_name = model.resolve_relation_name(target_field.to, model) return None if not related_model else related_model.pk
model_id = model.pk
elif issubclass(target_field, ManyToManyField):
is_multi = True
field_name = model.resolve_relation_name(target_field.through, model)
model_id = model.pk
else:
is_multi = False
related_name = model.resolve_relation_name(model, target_field.to)
related_model = getattr(model, related_name)
if not related_model:
return is_multi, None, None
model_id = related_model.pk
field_name = target_field.to.Meta.pkname
return is_multi, field_name, model_id
@staticmethod @staticmethod
def _get_group_field_name( def _get_related_field_name(
target_field: Type["BaseField"], model: Union["Model", Type["Model"]] target_field: Type["BaseField"], model: Union["Model", Type["Model"]]
) -> str: ) -> str:
if issubclass(target_field, ManyToManyField): if issubclass(target_field, ManyToManyField):
@ -202,13 +187,15 @@ class PrefetchQuery:
for related in related_to_extract: for related in related_to_extract:
target_field = model.Meta.model_fields[related] target_field = model.Meta.model_fields[related]
target_model = target_field.to.get_name() target_model = target_field.to.get_name()
is_multi, field_name, model_id = self._get_model_id_and_field_name( model_id = self._get_model_id(target_field=target_field, model=model)
if model_id is None: # pragma: no cover
continue
field_name = self._get_related_field_name(
target_field=target_field, model=model target_field=target_field, model=model
) )
if field_name is None or model_id is None: # pragma: no cover
continue
children = self.already_extracted.get(target_model, {}).get(field_name, {}) children = self.already_extracted.get(target_model, {}).get(field_name, {})
self._set_children_on_model( self._set_children_on_model(
model=model, related=related, children=children, model_id=model_id model=model, related=related, children=children, model_id=model_id
@ -384,7 +371,7 @@ class PrefetchQuery:
) -> None: ) -> None:
target_model = target_field.to target_model = target_field.to
for row in rows: for row in rows:
field_name = self._get_group_field_name( field_name = self._get_related_field_name(
target_field=target_field, model=parent_model target_field=target_field, model=parent_model
) )
item = target_model.extract_prefixed_table_columns( item = target_model.extract_prefixed_table_columns(