Skip to content

Add missing _default_manager symbol to generated through model #1745

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
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
6 changes: 6 additions & 0 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ def run(self) -> None:
name="objects",
sym_type=Instance(manager_info, [Instance(through_model, [])]),
)
# Also add manager as '_default_manager' attribute
helpers.add_new_sym_for_info(
through_model,
name="_default_manager",
sym_type=Instance(manager_info, [Instance(through_model, [])]),
)

@cached_property
def default_pk_instance(self) -> Instance:
Expand Down
5 changes: 4 additions & 1 deletion tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
from myapp.models import User
reveal_type(User().friends) # N: Revealed type is "django.db.models.fields.related_descriptors.ManyRelatedManager[myapp.models.User]"
reveal_type(User.friends.through.objects.get()) # N: Revealed type is "myapp.models.User_friends"
reveal_type(User.friends.through._default_manager) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.User_friends]"
reveal_type(User.friends.through().from_user) # N: Revealed type is "myapp.models.User"
reveal_type(User.friends.through().from_user_id) # N: Revealed type is "builtins.int"
reveal_type(User.friends.through().to_user) # N: Revealed type is "myapp.models.User"
Expand Down Expand Up @@ -1088,6 +1089,7 @@

reveal_type(MyModel.auto_through.through)
reveal_type(MyModel.auto_through.through.mymodel)
reveal_type(MyModel.auto_through.through._default_manager)
Copy link
Member

Choose a reason for hiding this comment

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

What about other m2m fields?

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean in addition to _default_manager? Or which fields?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ping @sobolevn? This solves the issue I had.


reveal_type(MyModel.other_again.through)
out: |
Expand All @@ -1106,7 +1108,8 @@
main:17: note: Revealed type is "builtins.int"
main:19: note: Revealed type is "Type[myapp.models.MyModel_auto_through]"
main:20: note: Revealed type is "django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor[django.db.models.fields.related.ForeignKey[Union[myapp.models.MyModel, django.db.models.expressions.Combinable], myapp.models.MyModel]]"
main:22: note: Revealed type is "Type[myapp.models.MyModel_other_again]"
main:21: note: Revealed type is "django.db.models.manager.Manager[myapp.models.MyModel_auto_through]"
main:23: note: Revealed type is "Type[myapp.models.MyModel_other_again]"
installed_apps:
- myapp
files:
Expand Down