MCPcopy
hub / github.com/ormar-orm/ormar / prefetch_related

Method prefetch_related

ormar/queryset/queries/prefetch_query.py:489–515  ·  view source on GitHub ↗

Main entry point for prefetch_query. Receives list of already initialized parent models with all children from select_related already populated. Receives also list of row sql result rows as it's quicker to extract ids that way instead of calling each model.

(self, models: Sequence["Model"])

Source from the content-addressed store, hash-verified

487 self.load_tasks: list[Node] = []
488
489 async def prefetch_related(self, models: Sequence["Model"]) -> Sequence["Model"]:
490 """
491 Main entry point for prefetch_query.
492
493 Receives list of already initialized parent models with all children from
494 select_related already populated. Receives also list of row sql result rows
495 as it's quicker to extract ids that way instead of calling each model.
496
497 Returns list with related models already prefetched and set.
498
499 :param models: list of already instantiated models from main query
500 :type models: Sequence[Model]
501 :param rows: row sql result of the main query before the prefetch
502 :type rows: list[sqlalchemy.engine.result.RowProxy]
503 :return: list of models with children prefetched
504 :rtype: list[Model]
505 """
506 parent_task = RootNode(models=cast(list["Model"], models))
507 self._build_load_tree(
508 prefetch_dict=self.prefetch_dict,
509 select_dict=self.select_dict,
510 parent=parent_task,
511 model=self.model,
512 )
513 await parent_task.load_data()
514 parent_task.reload_tree()
515 return parent_task.models
516
517 def _build_load_tree(
518 self,

Callers 1

Calls 4

_build_load_treeMethod · 0.95
reload_treeMethod · 0.95
RootNodeClass · 0.85
load_dataMethod · 0.45

Tested by

no test coverage detected