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
pythonversion: 3.11djangoversion: 3.2.16mypyversion: 0.991django-stubsversion: 1.14.0django-stubs-extversion: 0.7.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 19 (5 by maintainers)
I was able to fix this issue by annotating the related name. In this case it would be
I found I had the same issue for plain
Modelclasses, and the same problem with related managers just not working.Ended up (in pyright) just setting
reportAttributeAccessIssuetofalseas the number of error messages made attribute checking functionally useless (WithAnnotationsalso appears broken, but I think that’s a different bug).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
to
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.RelatedManagerworkaround 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?Annotating with
RelatedManager[Bar]fromdjango.db.models.manager.RelatedManageralso 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?