Skip to content

Prefetch's to_attr raises "Model" has no attribute "prefetched_field"Β #795

@Crocmagnon

Description

@Crocmagnon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions