Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add a <a href="https://docs.djangoproject.com/en/1.4/ref/models/fields/#django.db.models.ForeignKey.related_name" rel="nofollow"><code>related_name</code></a> to the foreign key, then use the <a href="https://docs.djangoproject.com/en/1.4/ref/models/querysets/#prefetch-related" rel="nofollow"><code>prefetch_related</code></a> method which added to Django 1.4:</p> <blockquote> <p>Returns a <code>QuerySet</code> that will automatically retrieve, in a single batch, related objects for each of the specified lookups.</p> <p>This has a similar purpose to <code>select_related</code>, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different:</p> <ul> <li><p><code>select_related</code> works by creating a SQL join and including the fields of the related object in the <code>SELECT</code> statement. For this reason, <code>select_related</code> gets the related objects in the same database query. However, to avoid the much larger result set that would result from joining across a 'many' relationship, <code>select_related</code> is limited to single-valued relationships - foreign key and one-to-one.</p></li> <li><p><code>prefetch_related</code>, on the other hand, does a separate lookup for each relationship, and does the 'joining' in Python. This allows it to prefetch many-to-many and <strong>many-to-one objects</strong>, which cannot be done using <code>select_related</code>, in addition to the foreign key and one-to-one relationships that are supported by <code>select_related</code>. It also supports prefetching of <code>GenericRelation</code> and <code>GenericForeignKey</code>.</p></li> </ul> </blockquote> <pre><code>class Parent(models.Model): name = models.CharField(max_length=31) class Child(models.Model): name = models.CharField(max_length=31) parent = models.ForeignKey(Parent, related_name='children') &gt;&gt;&gt; Parent.objects.all().prefetch_related('children') </code></pre> <p>All the relevant children will be fetched in a single query, and used to make QuerySets that have a pre-filled cache of the relevant results. These QuerySets are then used in the <code>self.children.all()</code> calls.</p> <blockquote> <p><strong>Note 1</strong> that, as always with QuerySets, any subsequent chained methods which imply a different database query will ignore previously cached results, and retrieve data using a fresh database query.</p> <p><strong>Note 2</strong> that if you use <code>iterator()</code> to run the query, <code>prefetch_related()</code> calls will be ignored since these two optimizations do not make sense together.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload