Skip to content
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

Support select_related and prefetch_related for inherited models and custom queryset for child models #531

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

k4anubhav
Copy link

@k4anubhav k4anubhav commented Oct 13, 2022

add select_polymorphic_related, prefetch_polymorphic_related and custom_queryset methods for select and prefetch on inherited models

Triggers Django errors when that field doesn't exist in a particular subclass

Example works exactly same as django select_related and prefetch_related

queryset = Project.objects.select_polymorphic_related(
    ArtProject, 'artist', 'canvas__painter'
).select_polymorphic_related(
    ResearchProject, 'supervisor',
)


queryset = Project.objects.prefetch_polymorphic_related(
    ArtProject, 'artist', Prefetch('canvas', queryset=Project.objects.annotate(size=F('width') * F('height')))
).prefetch_polymorphic_related(
    ResearchProject, 'authors',
)


queryset = Project.objects.custom_queryset(ArtProject, ArtProject._base_objects.annotate(cost=F('cost') + F('canvas_cost'))))

@k4anubhav
Copy link
Author

#198

@k4anubhav k4anubhav changed the title Support select_related and prefetch_related for inherited models Support select_related and prefetch_related for inherited models and custom queryset for child models Nov 8, 2022
@0legRadchenko
Copy link

Hi! Thank you for your commits! =)
Right now there are some problems If Im trying to combine select_related and select_polymorphic_related in one query (in such case It dupicates queries). So its not possible to use them simultaneously without making extra duplicated queries.

But if I use select_polymorphic_related without select_related everything working fine and vice versa If I use select_related without select_polymorphic_related in one query everything working good as well.

I've sent you additional info in telegram :)

@0legRadchenko
Copy link

Now all is great 🙌🏻 Ive tested select_related and select_prefetch_related together and also separatly and did not manage to find any duplicates in queries and so far so good.

@Safrone
Copy link

Safrone commented Dec 6, 2022

Anything holding this back? Feels like it would be a big win if it "just works"

@alphatownsman
Copy link

it would be really really nice to have this

@mazulo
Copy link

mazulo commented Feb 24, 2023

Hey team, when can we expect to get this merged?

@FrancoisDupayrat
Copy link

Thanks a lot for this PR. It single-handedly solved the biggest performance issue in my production app.
I can confirm it works. Hopefully it will get merged.

pgammans added a commit to bva-icx/django-polymorphic that referenced this pull request Jun 26, 2023
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 added a commit to bva-icx/django-polymorphic that referenced this pull request Jun 26, 2023
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
@Safrone
Copy link

Safrone commented Sep 8, 2023

I created a Gist showing how to use this enhancement without having to pin this PR version or modify Django Polymorphic:
https://gist.github.com/Safrone/7c24b492a791679e6e94cc765459dc78

I haven't gotten to do extensive testing on it so anyone using it finding issues please let me know

andriilahuta pushed a commit to andriilahuta/django-polymorphic that referenced this pull request Dec 19, 2023
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 added a commit to bva-icx/django-polymorphic that referenced this pull request Apr 4, 2024
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 added a commit to bva-icx/django-polymorphic that referenced this pull request Apr 4, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants