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

hacks for getting around the select_related issue from parent on inherited models to avoid n+1 queries? #436

Open
drivelous opened this issue May 20, 2020 · 0 comments

Comments

@drivelous
Copy link

drivelous commented May 20, 2020

So I've read this issue extensively and tried some things out which don't work -- #198

So I'm trying to see if I can come to a middle ground where maybe I don't get the free select_related query but can add a prefetch_related and avoid n+1s. I have some models defined like this

class Message(PolymorphicModel):
    body = models.TextField
    # fields

class SMSMessage(Message)
    from_phone = models.CharField
    to_phone = models.ForeignKey(FooPhoneNumberModel)

class EmailMessage(Message):
    from_email = models.CharField
    to_email = models.ForeignKey(FooEmailModel)

class InstantMessage(Message):
    from_username = models.CharField
    to_username = models.ForeignKey(FooUser)

I'm writing a django testcase for a view that serializes all instances of subclassed messages for a particular user and this part of the test fails when adding more instances

        _create_messages()

        num_queries = 25
        with self.assertNumQueries(num_queries):
            response = self.client.get(url)
            payload = json.loads(response.content)
            self.assertEqual(response.status_code, 200)
            self.assertEqual(payload['has_more'], False)

        _create_messages()

        # fails -- does 45 queries
        with self.assertNumQueries(num_queries):
            response = self.client.get(url)

I was trying to mess around and see if I could run some type of prefetch? Something like:

messages = Message.objects.prefetch_related(
    Prefetch('smsmessage', queryset=SMSMessage.objects.filter(user=user), to_attr='through__smsmessage'),
    Prefetch('emailmessage', queryset=EmailMessage.objects.filter(user=user), to_attr='through__emailmessage'),
)

so that the query can execute the same amount of queries whether it's 20 messages or 1000 messages.

Any help is appreciated including explanations of the internals. I've been trying to do things like call select_related('smsmessage') and select_related('message_ptr__smsmessage') and the like and my mind is mush right now

@drivelous drivelous changed the title hacks for getting around the select_related issue from parent on inherited models? are there hacks for getting around the select_related issue from parent on inherited models to avoid n+1 queries? May 20, 2020
@drivelous drivelous changed the title are there hacks for getting around the select_related issue from parent on inherited models to avoid n+1 queries? hacks for getting around the select_related issue from parent on inherited models to avoid n+1 queries? May 20, 2020
pgammans added a commit to bva-icx/django-polymorphic that referenced this issue 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 issue 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
andriilahuta pushed a commit to andriilahuta/django-polymorphic that referenced this issue 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 issue 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 issue 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

No branches or pull requests

1 participant