db.models.prefetch_related_objects()

prefetch_related_objects(model_instances, *related_lookups) [source]

New in Django 1.10.

Prefetches the given lookups on an iterable of model instances. This is useful in code that receives a list of model instances as opposed to a QuerySet; for example, when fetching models from a cache or instantiating them manually.

Pass an iterable of model instances (must all be of the same class) and the lookups or Prefetch objects you want to prefetch for. For example:

>>> from django.db.models import prefetch_related_objects
>>> restaurants = fetch_top_restaurants_from_cache()  # A list of Restaurants
>>> prefetch_related_objects(restaurants, 'pizzas__toppings')
doc_Django
2016-10-09 18:36:03
Comments
Leave a Comment

Please login to continue.