- There is no support to prefetch related models to increase performance
- Query results need to be cached manually
--
Ticket URL: <https://code.djangoproject.com/ticket/28600>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => assigned
* owner: nobody => Adnan Umer
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:1>
* has_patch: 1 => 0
* stage: Unreviewed => Accepted
Comment:
Tentatively accepting, although I haven't looked into the history to
understand if there's a design reason why it's not implemented. A
[https://groups.google.com/forum/#!topic/django-users/98IZGnqtsYo post on
django-users] suggests that using `prefetch_related_objects()` (which has
since been [https://docs.djangoproject.com/en/dev/ref/models/querysets
/#prefetch-related-objects documented as a public API]) works.
I'm unchecking "Has patch" as I don't see a patch anywhere. If there's a
patch somewhere, please provide a link.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:2>
* has_patch: 0 => 1
* needs_tests: 0 => 1
Comment:
Here is the link to PR that enabled this feature
https://github.com/django/django/pull/9151
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:3>
Comment (by Дилян Палаузов):
Would you mind implementing the same for bulk_create() #28692?
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:4>
* cc: Дилян Палаузов (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:5>
Comment (by Adnan Umer):
Replying to [comment:4 Дилян Палаузов]:
> Would you mind implementing the same for bulk_create() #28692?
Once someone accept the ticket, I'll post commit solution of that. BTW you
can temporarily avoid that situation this way
{{{
models = ModelA.objects.select_related('b').bulk_create([ModelA(...),
ModelA(...), ModelA(...)])
from django.db.models import prefetch_related_objects
prefetch_related_objects(models, 'b')
for m in models:
print(m.b.description) # No more extra SELECT query here
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:6>
* needs_better_patch: 0 => 1
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:7>
* needs_tests: 1 => 0
Old description:
> When using custom raw SQL queries with Django ORM,
>
> - There is no support to prefetch related models to increase performance
> - Query results need to be cached manually
New description:
When using custom raw SQL queries with Django ORM,
- There is no support to prefetch related models to increase performance
- Query results need to be cached manually
PS: __len__ & __bool__ implementation can also be added as that was not
there because of no internal caching in RawQuerySet.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:8>
Comment (by Simon Charette):
Unless `__bool__` and `__len__` support is required for `prefetch_related`
to work they should be added/tracked in another PR/ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:9>
Comment (by Adnan Umer):
Replying to [comment:9 Simon Charette]:
> Unless `__bool__` and `__len__` support is required for
`prefetch_related` to work they should be added/tracked in another
PR/ticket.
Got that. Moving that to separate PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:10>
Old description:
> When using custom raw SQL queries with Django ORM,
>
> - There is no support to prefetch related models to increase performance
> - Query results need to be cached manually
>
> PS: __len__ & __bool__ implementation can also be added as that was not
> there because of no internal caching in RawQuerySet.
New description:
When using custom raw SQL queries with Django ORM,
- There is no support to prefetch related models to increase performance
- Query results need to be cached manually
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:11>
* needs_better_patch: 1 => 0
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:12>
* owner: Adnan Umer => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:13>
* owner: (none) => Adnan Umer
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:14>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:15>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:16>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:17>
* needs_better_patch: 1 => 0
Comment:
Did requested changes. Requesting to review the PR again.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:18>
* needs_docs: 0 => 1
Comment:
Patch is looking good. Just needs some adjustments to the documentation
changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:19>
* cc: Carlton Gibson (added)
Comment:
Replying to [comment:10 Adnan Umer]:
> Replying to [comment:9 Simon Charette]:
> > Unless `__bool__` and `__len__` support is required for
`prefetch_related` to work they should be added/tracked in another
PR/ticket.
>
> Got that. Moving that to separate PR.
Did that happen? (I
[https://github.com/django/django/pulls?utf8=✓&q=is%3Apr+author%3Auadnan
can't see another PR by Adnan])
I ask because the warning in
[https://github.com/django/django/blob/7a22d9f75125e3cfbea0979a876efe4634f6fe05/docs/topics/db/sql.txt#L88-L97
docs/db/sql.txt] about this will also need updating:
`__bool__()` and `__len__()` are not defined in `RawQuerySet`, and
thus all ``RawQuerySet`` instances are considered `True`. The reason
these methods are not implemented in `RawQuerySet` is that
implementing
them without internal caching would be a performance drawback and
adding
such caching would be backward incompatible.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:20>
Comment (by Adnan Umer):
Replying to [comment:20 Carlton Gibson]:
> Did that happen? (I
[https://github.com/django/django/pulls?utf8=✓&q=is%3Apr+author%3Auadnan
can't see another PR by Adnan])
I havn't yet opened any new PR for that. I was just waiting on
approval/merge of my existing ticket. Once that is done I'll create a new
PR for that.
> I ask because the warning in
[https://github.com/django/django/blob/7a22d9f75125e3cfbea0979a876efe4634f6fe05/docs/topics/db/sql.txt#L88-L97
docs/db/sql.txt] about this will also need updating:
Yes, I've created a new ticket https://code.djangoproject.com/ticket/29337
and assigned that to myself. Not sure should I open PR once this one got
merged or before merge?
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:21>
* needs_docs: 1 => 0
Comment:
Unchecking "Needs Documentation" to have PR reviewed again after doing
requested changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:22>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:23>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"534d8d875eebac6aee278f0ffa6cc59760dac546" 534d8d87]:
{{{
#!CommitTicketReference repository=""
revision="534d8d875eebac6aee278f0ffa6cc59760dac546"
Fixed #28600 -- Added prefetch_related() support to RawQuerySet.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:24>
Comment (by Дилян Палаузов):
Would you mind implementing the same for bulk_create() #28692?
--
Ticket URL: <https://code.djangoproject.com/ticket/28600#comment:25>