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
Related polymorphic query support inc select_related and prefetch_related #545
Open
pgammans
wants to merge
15
commits into
jazzband:master
Choose a base branch
from
bva-icx:select_and_prefetch_related
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
@pgammans Thanks, this seems to be very useful improvement. Also I thing part of this PR should be change of documentation to reflect this, e.g.: https://django-polymorphic.readthedocs.io/en/stable/advanced.html?highlight=select_related#restrictions-caveats |
pgammans
force-pushed
the
select_and_prefetch_related
branch
from
April 4, 2024 14:59
5eb5e3e
to
89ea07b
Compare
implement support for a single query for select related base fetches across polymorphic models. adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models. fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244 possible fixes: jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names related: jazzband#531
pgammans
force-pushed
the
select_and_prefetch_related
branch
from
April 4, 2024 18:24
94243ad
to
eae92aa
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request add support for select related and general related queries.
Summary
To take advantage of this it requires that the plain Django model either adds a custom query set manager and uses that for queries where it wants the related object to be transmogrify into the final polymorphic type of
that the default manager's queryset uses/ has PolymorphicQuerySetMixin or PolymorphicRelatedQuerySet as a base class
ie
class PlainModel(models.Model): objects = models.Manager.from_queryset(PolymorphicRelatedQuerySet)()
The default Django select_related can be used including which means this is simple to use from the Django admin.
Performance
When using select_related we archive fetch of the data in a single query including all information required to transmogrify the related object. This is an improvement from n+1 (main line) or models + 1 (with #531)
Notes
It has minimal support for prefetch_related cannot fetch attributes not on all child models or via class names this may fix #498
Fixes: #198 #436 #359 #244