Opened 6 years ago

Closed 6 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 by Simon Charette, 6 years ago

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 by obayemi, 6 years ago

Summary: prefetch_related on GenericRelations notprefetch_related on GenericRelations

comment:3 by Tim Graham, 6 years ago

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

comment:4 by Tom Forbes, 6 years ago

Owner: changed from nobody to Tom Forbes
Status: newassigned

comment:5 by Tom Forbes, 6 years ago

Cc: Tom Forbes added

comment:6 by Tom Forbes, 6 years ago

Has patch: set

comment:7 by Carlton Gibson, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Tim Graham <timograham@…>, 6 years ago

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