django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Prefetch's to_attr raises "Model" has no attribute "prefetched_field"

Open Crocmagnon opened this issue 3 years ago • 3 comments

Bug report

What's wrong

class Tag(models.Model):
    pass

class Article(models.Model):
    tags = models.ManyToManyField(to=Tag, related_name="articles", blank=True)

    def method(self):
        published_articles = Article.objects.exclude(pk=self.pk)
        for tag in self.tags.all().prefetch_related(
            Prefetch("articles", published_articles, to_attr="published_articles")
        ):
            print(tag.published_articles)
src/articles/models.py:147: error: "Tag" has no attribute "published_articles"

How is that should be

published_articles should be recognized as a valid attribute for tag in the loop, as defined in the to_attr of the Prefetch instance.

System information

  • OS: macOS 12.1 (21C52)
  • python version: Python 3.10.0
  • django version: 4.0
  • mypy version: mypy 0.930
  • django-stubs version: 1.9.0
  • django-stubs-ext version: 0.3.1

Crocmagnon avatar Dec 28 '21 17:12 Crocmagnon