Opened 8 years ago
Last modified 8 years ago
#28600 closed New feature
Add prefetch related support to RawQuerySet — at Version 11
| Reported by: | Adnan Umer | Owned by: | Adnan Umer |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Дилян Палаузов, Carlton Gibson | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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
Change History (11)
comment:1 by , 8 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 8 years ago
| Has patch: | unset |
|---|---|
| Summary: | Prefetch Related Support in RawQuerySet → Add prefetch related support to RawQuerySet |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 8 years ago
| Has patch: | set |
|---|---|
| Needs tests: | set |
Here is the link to PR that enabled this feature.
follow-up: 6 comment:4 by , 8 years ago
Would you mind implementing the same for bulk_create() #28692?
comment:5 by , 8 years ago
| Cc: | added |
|---|
comment:6 by , 8 years ago
Replying to Дилян Палаузов:
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.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
comment:7 by , 8 years ago
| Needs documentation: | set |
|---|---|
| Patch needs improvement: | set |
comment:8 by , 8 years ago
| Description: | modified (diff) |
|---|---|
| Needs tests: | unset |
follow-up: 10 comment:9 by , 8 years ago
Unless __bool__ and __len__ support is required for prefetch_related to work they should be added/tracked in another PR/ticket.
comment:10 by , 8 years ago
Replying to Simon Charette:
Unless
__bool__and__len__support is required forprefetch_relatedto work they should be added/tracked in another PR/ticket.
Got that. Moving that to separate PR.
comment:11 by , 8 years ago
| Description: | modified (diff) |
|---|
Tentatively accepting, although I haven't looked into the history to understand if there's a design reason why it's not implemented. A post on django-users suggests that using
prefetch_related_objects()(which has since been 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.