django-stubs: error: Couldn't resolve related manager for relation on python3.11 and django3.2

Bug report

What’s wrong

I got Couldn't resolve related manager for relation error when using python3.11 and django3.2. So the model looks like

# dummy/apps/foos/models.py

class Foo(models.Model):
    ....

# dummy/apps/bars/models.py

class BarQueryset(models.Queryset):
    ...

class Bar(models.Model):
    ...
   foo = models.Foreignkey('foos.Foo', related_name='bars')
   objects = models.Manager.from_queryset(BarQueryset)()

I will get the following error: dummy/apps/foos/models.py:###: error: Couldn't resolve related manager for relation 'bars' (from dummy.apps.bars.models.Bar.bars.Bar.foo). [django-manager-missing]

It seems only happen when the reverse foreign key has a custom objects like Bar above… And may be related to #1285

I have to use # type: ignore for now to silent this…

How is that should be

Should have no problem. I tried to downgrade python back to python3.9, mypy==0.812, and django-stubs==1.8.0 and it has no issue.

System information

  • OS: ubuntu 20
  • python version: 3.11
  • django version: 3.2.16
  • mypy version: 0.991
  • django-stubs version: 1.14.0
  • django-stubs-ext version: 0.7.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 19 (5 by maintainers)

Most upvoted comments

I was able to fix this issue by annotating the related name. In this case it would be

class Foo:
    field1 = models.PositiveSmallIntegerField()
    bars: "QuerySet[Bar]"
    # or if related_name isn't set:
    bar_set: "QuerySet[Bar]"

I found I had the same issue for plain Model classes, and the same problem with related managers just not working.

Ended up (in pyright) just setting reportAttributeAccessIssue to false as the number of error messages made attribute checking functionally useless (WithAnnotations also appears broken, but I think that’s a different bug).

I was able to fix this issue by annotating the related name. In this case it would be

class Foo:
    field1 = models.PositiveSmallIntegerField()
    bars: "QuerySet[Bar]"
    # or if related_name isn't set:
    bar_set: "QuerySet[Bar]"

Annotating with RelatedManager[Bar] from django.db.models.manager.RelatedManager also works.

This would throw Module "django.db.models.manager" has no attribute "RelatedManager" , correct me if I’m wrong.

@danjac I have exactly the same issue.

For some reason, it could be worked around by changing

class MyModel(TimeStampedModel):
     ...

to


class _TSM(models.Models if TYPE_CHECKING else TimeStampedModel):
    class Meta:
        abstract = True

class MyModel(_TSM):
     ...

but that doesn’t seem reasonable, I would prefer to wait until this is fixed (or reverted, the only thing I wanted from 5.0.4 is mypy 1.11 support).

Update to 5.0.4 also breaks some related managers resolution for models without inheritance, I can’t clarify the reason as for now

Not sure why this was closed, but I have run into the same issue, even trying the django_stubs_ext.db.models.manager.RelatedManager workaround suggested (Python 3.12, django 5.1, django-stubs 5.0.4).

This is with models subclassing TimeStampedModel : it seems the only solution is to just not use this base class?

I was able to fix this issue by annotating the related name. In this case it would be

class Foo:
    field1 = models.PositiveSmallIntegerField()
    bars: "QuerySet[Bar]"
    # or if related_name isn't set:
    bar_set: "QuerySet[Bar]"

Annotating with RelatedManager[Bar] from django.db.models.manager.RelatedManager also works.

I didn’t look into this in detail, but maybe it helps to add manual type hints to the Model class for these manager attributes?