-
-
Notifications
You must be signed in to change notification settings - Fork 485
Improve hints for ReverseOneToOneDescriptor
and start using it
#1733
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
sobolevn
merged 6 commits into
typeddjango:master
from
flaeppe:fix/reverse-one-to-one-descriptor
Sep 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ac63e9
Improve hints for `ReverseOneToOneDescriptor` and start using it
flaeppe 729cf6e
fixup! Improve hints for `ReverseOneToOneDescriptor` and start using it
flaeppe 52b19a6
fixup! fixup! Improve hints for `ReverseOneToOneDescriptor` and start…
flaeppe d602f31
fixup! fixup! fixup! Improve hints for `ReverseOneToOneDescriptor` an…
flaeppe f1daa5c
fixup! fixup! fixup! fixup! Improve hints for `ReverseOneToOneDescrip…
flaeppe 93c911b
fixup! fixup! fixup! fixup! fixup! Improve hints for `ReverseOneToOne…
flaeppe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,7 +440,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None: | |
self.add_new_node_to_model_class("_default_manager", default_manager, is_classvar=True) | ||
|
||
|
||
class AddRelatedManagers(ModelClassInitializer): | ||
class AddReverseLookups(ModelClassInitializer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me quite some time to figure out that the reverse one to one was set by the plugin in this initializer. I'm all up for an even better name that describes that this handles the reverse parts of relationships.. |
||
def get_reverse_manager_info(self, model_info: TypeInfo, derived_from: str) -> Optional[TypeInfo]: | ||
manager_fullname = helpers.get_django_metadata(model_info).get("reverse_managers", {}).get(derived_from) | ||
if not manager_fullname: | ||
|
@@ -455,6 +455,9 @@ def set_reverse_manager_info(self, model_info: TypeInfo, derived_from: str, full | |
helpers.get_django_metadata(model_info).setdefault("reverse_managers", {})[derived_from] = fullname | ||
|
||
def run_with_model_cls(self, model_cls: Type[Model]) -> None: | ||
reverse_one_to_one_descriptor = self.lookup_typeinfo_or_incomplete_defn_error( | ||
fullnames.REVERSE_ONE_TO_ONE_DESCRIPTOR | ||
) | ||
# add related managers | ||
for relation in self.django_context.get_model_relations(model_cls): | ||
attname = relation.get_accessor_name() | ||
|
@@ -474,7 +477,13 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None: | |
continue | ||
|
||
if isinstance(relation, OneToOneRel): | ||
self.add_new_node_to_model_class(attname, Instance(related_model_info, [])) | ||
related_model_instance = Instance(related_model_info, []) | ||
self.add_new_node_to_model_class( | ||
attname, | ||
Instance( | ||
reverse_one_to_one_descriptor, [Instance(self.model_classdef.info, []), related_model_instance] | ||
), | ||
) | ||
continue | ||
|
||
if isinstance(relation, ForeignObjectRel): | ||
|
@@ -732,7 +741,7 @@ def process_model_class(ctx: ClassDefContext, django_context: DjangoContext) -> | |
AddRelatedModelsId, | ||
AddManagers, | ||
AddDefaultManagerAttribute, | ||
AddRelatedManagers, | ||
AddReverseLookups, | ||
AddExtraFieldMethods, | ||
AddMetaOptionsAttribute, | ||
MetaclassAdjustments, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plugin should probably refine
def get_queryset()
s return type to be from the default manager of the related model. Can be seen here:https://github.com/django/django/blob/574ee4023e15cfb195833edfbaed353f8021c62f/django/db/models/fields/related_descriptors.py#L443-L444
I wanted to keep the change set smaller, so we can do this in a subsequent PR. As we already do a bit of changes and everything in here should be improvements. In general I think the usage is quite small for
get_queryset
in here anyways..