title: Django: Retrieving IDs of manyToMany fields quickly
body: <p>I have the following model schema in Django (with Postgres). </p>
  
  <pre><code>class A(Models.model):
      related = models.ManyToManyField("self", null=True)
  </code></pre>
  
  <p>Given a QuerySet of A, I would like to return a dictionary mapping each instance of <code>A</code> in the QuerySet to a list of <code>id</code>s of its <code>related</code> instances as quickly as possible.</p>
  
  <p>I can surely iterate through each A and query the related field, but is there a more optimal way?</p>