title: Type Hint for Django model with annotated field
body: <p>Let's say I have the following Django models:</p>
  <pre><code>class Toolbox(models.Model):
      name = models.CharField(max_length=255)
      tools = models.ManyToManyField(&quot;Tool&quot;)
  
  class Tool(models.Model):
      class Size(models.TextChoices):
          SMALL = &quot;S&quot;
          MEDIUM = &quot;M&quot;
          LARGE = &quot;L&quot;
  
      name = models.CharField(max_length=255)
      size = models.CharField(max_length=10, choices=Size.choices)
  </code></pre>
  <p>I have a function to get all small tools for each toolbox. The argument type hint comes from <a href="https://stackoverflow.com/a/44321011/6611672">this SO answer</a>:</p>
  <pre><code>from django.db.models import QuerySet
  
  def get_toolbox_to_small_tools_mappings(
      toolboxes: QuerySet | list[Toolbox],
  ) -&gt; dict[Toolbox, list[Tool]]:
      return {toolbox: toolbox.small_tools for toolbox in toolboxes}
  </code></pre>
  <p>The idea here is to require users of this function to prefetch this <code>small_tools</code> field using <code>prefetch_related()</code> to reduce the number of db hits and speed up the code:</p>
  <pre><code>toolboxes = Toolbox.objects.prefetch_related(
      Prefetch(
          &quot;tools&quot;,
          queryset=Tool.objects.filter(size=Tool.Size.SMALL),
          to_attr=&quot;small_tools&quot;,
      )
  )
  toolbox_to_small_tools_mappings = get_toolbox_to_small_tools_mappings(toolboxes)
  </code></pre>
  <p>This all works great but mypy is complaining with the following error:</p>
  <blockquote>
  <p>error: &quot;Toolbox&quot; has no attribute &quot;small_tools&quot;  [attr-defined]</p>
  </blockquote>
  <p>Is there anyway to fix this?</p>
  <p>The <code>WithAnnotations[Model]</code> type from django-subs (<a href="https://github.com/typeddjango/django-stubs#how-do-i-annotate-cases-where-i-called-querysetannotate" rel="nofollow noreferrer">see here</a>) is an option but it's <a href="https://github.com/typeddjango/django-stubs/issues/763" rel="nofollow noreferrer">buggy</a>.</p>