Fix prefetching in DeferringManyRelatedManager._apply_rel_filters()#203
Open
bbliem wants to merge 1 commit intowagtail:mainfrom
Open
Fix prefetching in DeferringManyRelatedManager._apply_rel_filters()#203bbliem wants to merge 1 commit intowagtail:mainfrom
bbliem wants to merge 1 commit intowagtail:mainfrom
Conversation
Suppose we have a `ParentalManyToManyField` and we use `prefetch_related` with a lookup queryset. Django's `prefetch_one_level()` in `django.db.models.query` iterates over a list of instances. For each of them, it calls `DeferringManyRelatedManager._apply_rel_filters(lookup.queryset)` and thus obtains a queryset, for which `prefetch_one_level()` then sets the attribute `_result_cache`. This is currently buggy in modelcluster as `DeferringManyRelatedManager._apply_rel_filters()` directly returns the given queryset instead of a copy. When `_apply_rel_filters(lookup.queryset)` is called twice, the second call returns the same object as the first. This is a problem because, in the second loop iteration of `prefetch_one_level()`, the cache for the first object will be overwritten by the cache for the second object. In fact, all objects will end up with the same cache -- the one of the last object. The current commit fixes this by making `DeferringManyRelatedManager._apply_rel_filters()` return a copy of the given queryset. It also adds a unit test. Note that basically the same problem was already fixed in wagtail#130 for `RelatedManager`, but apparently applying the fix to `DeferringManyRelatedManager` has been forgotten.
a338d5c to
174a4e1
Compare
bbliem
added a commit
to kausaltech/kausal-watch
that referenced
this pull request
Oct 29, 2025
This should be removed once this pull request is merged to modelcluster: wagtail/django-modelcluster#203
juyrjola
pushed a commit
to kausaltech/kausal-watch-old
that referenced
this pull request
Nov 14, 2025
This should be removed once this pull request is merged to modelcluster: wagtail/django-modelcluster#203
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Suppose we have a
ParentalManyToManyFieldand we useprefetch_relatedwith a lookup queryset. Django'sprefetch_one_level()indjango.db.models.queryiterates over a list of instances. For each of them, it callsDeferringManyRelatedManager._apply_rel_filters(lookup.queryset)and thus obtains a queryset, for whichprefetch_one_level()then sets the attribute_result_cache.This is currently buggy in modelcluster as
DeferringManyRelatedManager._apply_rel_filters()directly returns the given queryset instead of a copy. When_apply_rel_filters(lookup.queryset)is called twice, the second call returns the same object as the first. This is a problem because, in the second loop iteration ofprefetch_one_level(), the cache for the first object will be overwritten by the cache for the second object. In fact, all objects will end up with the same cache -- the one of the last object.The current commit fixes this by making
DeferringManyRelatedManager._apply_rel_filters()return a copy of the given queryset. It also adds a unit test.Note that basically the same problem was already fixed in #130 for
RelatedManager, but apparently applying the fix toDeferringManyRelatedManagerhas been forgotten.