title: How can I get related objects for related objects of an object in Django in a single query when all linked using Generic Foreign Key
body: <p>Assuming I have the following models and knowing that each Message can have multiple Records and each Record can have multiple Notes, how can I get a queryset of related Notes from a Message in a single query?</p>
  <pre><code>class Note:
      content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
      object_id = models.PositiveIntegerField()
      content_object = GenericForeignKey('content_type', 'object_id')
  
  
  class Record:
      content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
      object_id = models.PositiveIntegerField(null=True, blank=True)
      content_object = GenericForeignKey()
  
      notes = GenericRelation(Note, related_query_name='records')
  
  
  class Message:
      records = GenericRelation(Record, related_query_name='messages')
  </code></pre>
  <p>I know how to achieve this by using list comprehensions like shown below, however it is a bad approach since this will do multiple queries.</p>
  <pre><code>if records := message.records.all():
      notes = list(chain.from_iterable([record.notes.all() for record in records]))
  </code></pre>