You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When applying prefetch_related with a given queryset , the results of the related manager on the returned instances do return the wrong results if .filter(..) is applied instead of the (cached) .all() query.
Probably anything than .all() will return wrong results.
Unfortunately I could not really figure out where the bug exactly is located, because I'm too unfamiliar with this part of the django code base, but I guess it is related to the modelcluster.fields.ChildObjectsDescriptor.
This issue is related to and probably a duplicate of #103, but I narrowed down the steps to reproduce the bug and I hope this will get more attention then.
classTestPrefetch(TestCase):
defsetUp(self):
# Create parentsparent_1=Parent.objects.create()
parent_2=Parent.objects.create()
# Create a children for each parentChild.objects.create(parent=parent_1)
Child.objects.create(parent=parent_2)
deftest_prefetch(self):
parents=Parent.objects.prefetch_related(
Prefetch("children", queryset=Child.objects.all())
)
# Select an instanceparent_1=parents[0]
self.assertListEqual(
list(parent_1.children.filter()),
list(parent_1.children.all()),
)
Expected Result
parent_1.children.filter() and parent_1.children.all() should return the same queryset containing only the children of the instance parent_1.
Actual Result
parent_1.children.filter() returns all children in the database, also the one related to parent_2 only. Any further filtering is applied to this queryset and will yield wrong results.
The text was updated successfully, but these errors were encountered:
maqnius
changed the title
Filtering on related manager when using prefetch_related in combination with select_related on ClusterableModel return wrong results
Filtering on related manager when using prefetch_related in combination with select_related on ClusterableModel returns wrong results
Jan 30, 2023
maqnius
changed the title
Filtering on related manager when using prefetch_related in combination with select_related on ClusterableModel returns wrong results
Filtering on related manager when using prefetch_related in combination with select_related on a ClusterableModel returns wrong results
Jan 30, 2023
maqnius
changed the title
Filtering on related manager when using prefetch_related in combination with select_related on a ClusterableModel returns wrong results
Filtering on related manager when using prefetch_related with explicit queryset on a ClusterableModel returns wrong results
Jan 30, 2023
When applying
prefetch_related
with a givenqueryset
, the results of the related manager on the returned instances do return the wrong results if.filter(..)
is applied instead of the (cached).all()
query.Probably anything than
.all()
will return wrong results.Unfortunately I could not really figure out where the bug exactly is located, because I'm too unfamiliar with this part of the django code base, but I guess it is related to the
modelcluster.fields.ChildObjectsDescriptor
.This issue is related to and probably a duplicate of #103, but I narrowed down the steps to reproduce the bug and I hope this will get more attention then.
Steps to Reproduce
Models
Test
Expected Result
parent_1.children.filter()
andparent_1.children.all()
should return the same queryset containing only the children of the instanceparent_1
.Actual Result
parent_1.children.filter()
returns all children in the database, also the one related toparent_2
only. Any further filtering is applied to this queryset and will yield wrong results.The text was updated successfully, but these errors were encountered: