Add prefetch_related() cache invalidation for GenericRelations
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)
Component: |
Database layer (models, ORM) → contrib.contenttypes
|
Keywords: |
GenericForeignKey GenericRelation Prefetch removed
|
Triage Stage: |
Unreviewed → Accepted
|
Version: |
2.0 → master
|
Summary: |
prefetch_related on GenericRelations not → prefetch_related on GenericRelations
|
Summary: |
prefetch_related on GenericRelations → Add prefetch_related() cache invalidation for GenericRelations
|
Owner: |
changed from nobody to Tom Forbes
|
Status: |
new → assigned
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
This is similar to #26706 but for generic relations. The patch can probably be heavily inspired by d30febb4e59b659e0d279c77f61f936c199a05b2.