Opened 3 years ago

Closed 3 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

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 Changed 3 years ago by Simon Charette

Component: Database layer (models, ORM)contrib.contenttypes
Keywords: GenericForeignKey GenericRelation Prefetch removed
Triage Stage: UnreviewedAccepted
Version: 2.0master

This is similar to #26706 but for generic relations. The patch can probably be heavily inspired by d30febb4e59b659e0d279c77f61f936c199a05b2.

comment:2 Changed 3 years ago by obayemi

Summary: prefetch_related on GenericRelations notprefetch_related on GenericRelations

comment:3 Changed 3 years ago by Tim Graham

Summary: prefetch_related on GenericRelationsAdd prefetch_related() cache invalidation for GenericRelations

comment:4 Changed 3 years ago by Tom Forbes

Owner: changed from nobody to Tom Forbes
Status: newassigned

comment:5 Changed 3 years ago by Tom Forbes

Cc: Tom Forbes added

comment:6 Changed 3 years ago by Tom Forbes

Has patch: set

comment:7 Changed 3 years ago by Carlton Gibson

Triage Stage: AcceptedReady for checkin

comment:8 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In c02d4737:

Fixed #29612 -- Added GenericRelation prefetch_related() cache invalidation.

Note: See TracTickets for help on using tickets.
Back to Top