Skip to content

Fix missing related managers on some models #902

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

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
self.add_new_node_to_model_class(
attname, Instance(default_reverse_manager_info, []), no_serialize=True
)
return
continue

# The reverse manager we're looking for doesn't exist. So we create it.
# The (default) reverse manager type is built from a RelatedManager and the default manager on the related model
Expand Down
89 changes: 89 additions & 0 deletions tests/typecheck/models/test_related_fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
- case: test_related_name_custom_manager
main: |
from app1.models import Model1
from app2.models import Model2

reveal_type(Model1().test) # N: Revealed type is "app3.models.Model3_RelatedManager"
reveal_type(Model2().test) # N: Revealed type is "app3.models.Model3_RelatedManager"
reveal_type(Model1().test2) # N: Revealed type is "app3.models.Model4_RelatedManager"
reveal_type(Model2().test2) # N: Revealed type is "app3.models.Model4_RelatedManager"
Comment on lines +8 to +9
Copy link
Contributor Author

@ljodal ljodal Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix these attributes will be missing, as it returns after adding the .test attributes

installed_apps:
- base
- users
- app1
- app2
- app3
files:
- path: base/__init__.py
- path: base/models.py
content: |
from django.db import models
class OwnedModel(models.Model):
owner = models.ForeignKey("users.User", on_delete=models.CASCADE)

class Meta:
abstract = True

- path: users/__init__.py
- path: users/models.py
content: |
from django.db import models
class User(models.Model):
pass

- path: app1/__init__.py
- path: app1/models.py
content: |
from django.db import models
from base.models import OwnedModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app3.models import Model3

class CustomQuerySet(models.QuerySet):
pass

Model1Manager = models.Manager.from_queryset(CustomQuerySet)
class Model1(OwnedModel):
objects = Model1Manager()

- path: app2/__init__.py
- path: app2/models.py
content: |
from django.db import models
from base.models import OwnedModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app3.models import Model3

class CustomQuerySet(models.QuerySet):
pass

Model2Manager = models.Manager.from_queryset(CustomQuerySet)
class Model2(OwnedModel):
objects = Model2Manager()

- path: app3/__init__.py
- path: app3/models.py
content: |
from django.db import models
from app1.models import Model1
from app2.models import Model2
from base.models import OwnedModel

class CustomQuerySet(models.QuerySet):
pass

Model3Manager = models.Manager.from_queryset(CustomQuerySet)
class Model3(OwnedModel):
a = models.ForeignKey(Model1, related_name="test", on_delete=models.CASCADE)
b = models.ForeignKey(Model2, related_name="test", on_delete=models.CASCADE)

objects = Model3Manager()

Model4Manager = models.Manager.from_queryset(CustomQuerySet)
class Model4(OwnedModel):
a = models.ForeignKey(Model1, related_name="test2", on_delete=models.CASCADE)
b = models.ForeignKey(Model2, related_name="test2", on_delete=models.CASCADE)

objects = Model4Manager()