#19420 closed Bug (fixed)
prefetch_related does not cache results when the model is inherited
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.4 |
| Severity: | Normal | Keywords: | prefetch_related inheritence |
| Cc: | antonbaklanov@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
given the following models:
class ParentBase(models.Model): pass
class Parent(models.Model): pass
class ParentWithBase(ParentBase): pass
class Child(models.Model):
parent = models.ForeignKey('Parent')
parent_with_base = models.ForeignKey('ParentWithBase')
When I try to select a child model and prefetch the parents, the parent with the base performs another query which I am trying to avoid.
So my test code is:
# Create the child and parent objects
Child.objects.create(parent = Parent.objects.create(), parent_with_base = ParentWithBase.objects.create())
# Select the child and prefetch the parents
child = Child.objects.prefetch_related('parent','parent_with_base').all()[0]
with self.assertNumQueries(0):
child.parent # This works just fine - no queries are emitted
with self.assertNumQueries(0):
child.parent_with_base # TEST FAILS HERE - since one query is emitted
Change History (4)
comment:1 by , 13 years ago
| Cc: | added |
|---|
comment:2 by , 13 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | new → closed |
comment:3 by , 13 years ago
Good to hear it works in 1.5, but worksforme is not the right resolution, since it is broken on 1.4 as reported.
comment:4 by , 13 years ago
| Resolution: | worksforme → fixed |
|---|
Note:
See TracTickets
for help on using tickets.
I put your test models and test code into a test project, and it failed under 1.4, but passes under 1.5b2. It appears this bug has been fixed since the release of 1.4.