title: Following subclass relationships with select_related
body: <p>I have a multi-table inheritance schema similar to the following:</p>
  
  <pre><code>class NodeData(models.Model):
      node = models.ForeignKey(Node, db_index = True)
      value = models.FloatField(default = 0)
      name = models.TextField(unique=True, blank=True)
  
  class Node(models.Model):
      name = models.CharField(max_length=50, blank=True)
      description = models.TextField(blank=True)
      node_tree = models.ForeignKey(NodeTree, db_index = True)
      unique_name = models.TextField(unique=True, blank=True)
      last_updated_timestamp = models.DateTimeField('date last updated', blank=True)
  
  class ConceptNode(Node):
      node_parent = models.ForeignKey(Node, related_name="nodeParent", null=True, blank=True)
  
  class DerivedNode(Node):
      node_source = models.ForeignKey(Node, related_name="nodeSource")
      node_target = models.ForeignKey(Node, related_name="nodeTarget")
  </code></pre>
  
  <p>For performance reasons, I am using select_related(depth = 2) whenever I fetch a large selection of NodeData elements.  However, select_related does not follow relations down to subclasses, so the following code* results in a call to the ConceptNode implementation of myFunction with no pre-fetched data for the ConceptNode object used in that function:</p>
  
  <pre><code>nd = NodeData.objects.get(id = 1)
  nd.node.conceptnode.myFunction()
  </code></pre>
  
  <p>This makes select_related not so useful, since there are a lot of functions called this way, and they aren't reaping the benefits of the data cached by select_related.</p>
  
  <p><strong>My question is:</strong> can I get select_related to fetch this information for me, so that each NodeData object I get has both the Node and ConceptNode/DerivedNode instance cached?*</p>
  
  <p><em>NOTE: This is actually done using an accessor function model like I asked about <a href="https://stackoverflow.com/questions/11694883/how-does-determining-subclass-through-hasattr-trigger-a-db-query-in-djangos-orm">here</a></em></p>
  
  <p><em>NOTE: What I'm trying to do is similar to <a href="https://stackoverflow.com/questions/11020813/django-orm-how-to-use-select-related-on-the-foreign-key-of-a-subclass-from-i">this</a>, but is a bit different because I have multiple subclasses.</em></p>
  
  <p><strong>EDIT</strong>: Thanks to the tip from chris-platt, I found the following does what I need:</p>
  
  <pre><code>nd = NodeData.objects.select_related('conceptnode','derivednode').get(id = 1)
  nd.node.conceptnode.myFunction()
  </code></pre>
  
  <p>The first line pre-fetches the DerivedNode or ConceptNode instance into the relevant cache object.</p>