-
-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error: Couldn't resolve related manager for relation on python3.11 and django3.2 #1354
Comments
Are you sure you're getting typing on that setup? I introduced this error message a while back, but it was only to surface issues that were previously silently ignored. Basically, this error means that the plugin was not able to statically figure out what class the manager would be. Typically that means you're doing something in your code that's not statically checkable (or not supported by the plugin). Are you able to create a minimal example that reproduces the issue? |
ooh, right. I noticed something else, this will happen if the reverse FK class inherit from this 3rd party class https://github.com/jazzband/django-model-utils/blob/cced1c7aeaf97fa6ee5c7e44c8ea827ed10b2da5/model_utils/models.py#L19 so the simple code will looks like: # apps/foos/models.py
from django.db import models
class Foo(models.Model):
field1 = models.PositiveSmallIntegerField() # apps/bars/models.py
from django.db import models
from model_utils.models import TimeStampedModel
class BarQueryset(models.QuerySet):
def get_active_qs(self) -> models.QuerySet:
return self.filter(is_active=True)
class Bar(TimeStampedModel):
foo = models.ForeignKey("foos.Foo", related_name="bars", on_delete=models.CASCADE)
is_active = models.BooleanField(default=False)
objects = models.Manager.from_queryset(BarQueryset)() this will results in: removing the custom but, it will be fine if I changed the from django.db import models
class BarQueryset(models.QuerySet):
def get_active_qs(self) -> models.QuerySet:
return self.filter(is_active=True)
class BaseBar(models.Model):
name = models.CharField(max_length=255)
class Bar(BaseBar):
foo = models.ForeignKey("foos.Foo", related_name="bars", on_delete=models.CASCADE)
is_active = models.BooleanField(default=False)
objects = models.Manager.from_queryset(BarQueryset)() |
Ah, alright. I guess the error message could be improved here, but basically it appears that |
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? |
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 |
I deem that either any of the suggested approaches with explicit annotation is a resolution or if there's 3rd party code involved(ref: #1354 (comment)) this is a duplicate of #1023. Feel free to correct me if I'm wrong. |
This would throw |
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
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
likeBar
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
python
version: 3.11django
version: 3.2.16mypy
version: 0.991django-stubs
version: 1.14.0django-stubs-ext
version: 0.7.0The text was updated successfully, but these errors were encountered: