Opened 7 years ago
Closed 7 years ago
#29612 closed Bug (fixed)
Add prefetch_related() cache invalidation for GenericRelations
Reported by: | obayemi | Owned by: | Tom Forbes |
---|---|---|---|
Component: | contrib.contenttypes | Version: | dev |
Severity: | Normal | Keywords: | prefetch_related |
Cc: | Tom Forbes | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Pull Requests: | |||
Description ¶
with two models joined with a generic foreignkey / relationship, if we prefetch the generic relation, its cache will not be invalidated by updates (.add
, .remove
nor .all().delete()
)
class Foo(models.Model): tags = GenericRelation("TaggedItem") class TaggedItem(models.Model): tag = models.SlugField() content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey("content_type", "object_id")
Foo.objects.create() f = Foo.objects.prefetch_related('tags').last() a = TaggedItem(tag='tag') print(f.tags.all()) f.tags.add(a, bulk=False) print(f.tags.all())
will give
<QuerySet []> <QuerySet []>
Change History (8)
comment:1 by , 7 years ago
Component: | Database layer (models, ORM) → contrib.contenttypes |
---|---|
Keywords: | GenericForeignKey GenericRelation Prefetch removed |
Triage Stage: | Unreviewed → Accepted |
Version: | 2.0 → master |
comment:2 by , 7 years ago
Summary: | prefetch_related on GenericRelations not → prefetch_related on GenericRelations |
---|
comment:3 by , 7 years ago
Summary: | prefetch_related on GenericRelations → Add prefetch_related() cache invalidation for GenericRelations |
---|
comment:4 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 7 years ago
Cc: | added |
---|
'Heavily inspired' PR: https://github.com/django/django/pull/10277
comment:6 by , 7 years ago
Has patch: | set |
---|
comment:7 by , 7 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Note:
See TracTickets
for help on using tickets.
This is similar to #26706 but for generic relations. The patch can probably be heavily inspired by d30febb4e59b659e0d279c77f61f936c199a05b2.