Prefetch related objects
|Reported by:||Luke Plant||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Cc:||jdunck@…, cg@…, lists@…, django@…||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Related to #6432, here is a patch that adds a 'prefetch_related' method to
QuerySet. This method causes the requested related objects to be retrieved in a single batch when the main
QuerySet is evaluated.
The result is that the O(n) performance problem often related with M2M and reverse foreign key can be eliminated, at least for the simple but common case when you just want to use
.all() and not do further filtering. Obviously it should be used with care, like select_related.
Change History (42)
comment:23 Changed 5 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Accepted → Ready for checkin|