yahoo

Finished
145145 }
146146 else:
147147 query = {"%s__in" % self.field.related_query_name(): instances}
148 queryset = queryset.filter(**query)
148
149 # Handle sliced querysets by filtering in Python instead of SQL
150 if queryset.query.is_sliced:
151 # Evaluate the sliced queryset first
152 all_objects = list(queryset)
153 # Filter in Python based on the query conditions
154 if remote_field.is_hidden() or len(self.field.foreign_related_fields) == 1:
155 valid_ids = {instance_attr(inst)[0] for inst in instances}
156 filtered_objects = [
157 obj for obj in all_objects
158 if getattr(obj, related_field.name) in valid_ids
159 ]
160 else:
161 filtered_objects = [
162 obj for obj in all_objects
163 if getattr(obj, self.field.related_query_name()) in instances
164 ]
165 queryset = filtered_objects
166 else:
167 queryset = queryset.filter(**query)
149168
150169 # Since we're going to assign directly in the cache,
151170 # we must manage the reverse relation cache manually.
719738 instance_attr = self.field.get_foreign_related_value
720739 instances_dict = {instance_attr(inst): inst for inst in instances}
721740 query = {"%s__in" % self.field.name: instances}
722 queryset = queryset.filter(**query)
741
742 # Handle sliced querysets by filtering in Python instead of SQL
743 if queryset.query.is_sliced:
744 # Evaluate the sliced queryset first
745 all_objects = list(queryset)
746 # Filter in Python based on the query conditions
747 filtered_objects = [
748 obj for obj in all_objects
749 if getattr(obj, self.field.name) in instances
750 ]
751 queryset = filtered_objects
752 else:
753 queryset = queryset.filter(**query)
723754
724755 # Since we just bypassed this class' get_queryset(), we must manage
725756 # the reverse relation manually.
10521083 queryset = queryset.using(queryset._db or self._db)
10531084
10541085 query = {"%s__in" % self.query_field_name: instances}
1055 queryset = queryset._next_is_sticky().filter(**query)
1056
1057 # M2M: need to annotate the query in order to get the primary model
1058 # that the secondary model was actually related to. We know that
1059 # there will already be a join on the join table, so we can just add
1060 # the select.
1061
1062 # For non-autocreated 'through' models, can't assume we are
1063 # dealing with PK values.
1064 fk = self.through._meta.get_field(self.source_field_name)
1065 join_table = fk.model._meta.db_table
1066 connection = connections[queryset.db]
1067 qn = connection.ops.quote_name
1068 queryset = queryset.extra(
1069 select={
1070 "_prefetch_related_val_%s"
1071 % f.attname: "%s.%s"
1072 % (qn(join_table), qn(f.column))
1073 for f in fk.local_related_fields
1074 }
1075 )
1086
1087 # Handle sliced querysets by filtering in Python instead of SQL
1088 if queryset.query.is_sliced:
1089 # Evaluate the sliced queryset first
1090 all_objects = list(queryset)
1091 # Filter in Python based on the query conditions
1092 filtered_objects = [
1093 obj for obj in all_objects
1094 if getattr(obj, self.query_field_name) in instances
1095 ]
1096 queryset = filtered_objects
1097
1098 # For sliced querysets, we can't use extra() to add annotations
1099 # since we're working with a list of objects, not a queryset
1100 # We need to handle the annotation differently
1101 fk = self.through._meta.get_field(self.source_field_name)
1102 connection = connections[queryset.db if hasattr(queryset, 'db') else self._db]
1103
1104 def rel_obj_attr(result):
1105 # For M2M, we need to get the related value from the join table
1106 # Since we can't annotate, we'll need to use the relationship
1107 return tuple(
1108 getattr(result, f.attname)
1109 for f in fk.foreign_related_fields
1110 )
1111
1112 def instance_attr(inst):
1113 return tuple(
1114 f.get_db_prep_value(getattr(inst, f.attname), connection)
1115 for f in fk.foreign_related_fields
1116 )
1117 else:
1118 queryset = queryset._next_is_sticky().filter(**query)
1119
1120 # M2M: need to annotate the query in order to get the primary model
1121 # that the secondary model was actually related to. We know that
1122 # there will already be a join on the join table, so we can just add
1123 # the select.
1124
1125 # For non-autocreated 'through' models, can't assume we are
1126 # dealing with PK values.
1127 fk = self.through._meta.get_field(self.source_field_name)
1128 join_table = fk.model._meta.db_table
1129 connection = connections[queryset.db]
1130 qn = connection.ops.quote_name
1131 queryset = queryset.extra(
1132 select={
1133 "_prefetch_related_val_%s"
1134 % f.attname: "%s.%s"
1135 % (qn(join_table), qn(f.column))
1136 for f in fk.local_related_fields
1137 }
1138 )
1139
1140 def rel_obj_attr(result):
1141 return tuple(
1142 getattr(result, "_prefetch_related_val_%s" % f.attname)
1143 for f in fk.local_related_fields
1144 )
1145
1146 def instance_attr(inst):
1147 return tuple(
1148 f.get_db_prep_value(getattr(inst, f.attname), connection)
1149 for f in fk.foreign_related_fields
1150 )
1151
10761152 return (
10771153 queryset,
1078 lambda result: tuple(
1079 getattr(result, "_prefetch_related_val_%s" % f.attname)
1080 for f in fk.local_related_fields
1081 ),
1082 lambda inst: tuple(
1083 f.get_db_prep_value(getattr(inst, f.attname), connection)
1084 for f in fk.foreign_related_fields
1085 ),
1154 rel_obj_attr,
1155 instance_attr,
10861156 False,
10871157 self.prefetch_cache_name,
10881158 False,
Test NameStatus
test_foreignkey_reverse (prefetch_related.tests.PrefetchLimitTests)
Fail
test_m2m_forward (prefetch_related.tests.PrefetchLimitTests)
Fail
test_m2m_reverse (prefetch_related.tests.PrefetchLimitTests)
Fail
test_reverse_ordering (prefetch_related.tests.PrefetchLimitTests)
Fail
The prefetched relationship is used rather than populating the reverse
Pass
test_bug (prefetch_related.tests.Ticket19607Tests)
Pass
When intermediary results are prefetched without a destination
Pass
In-bulk does correctly prefetch objects by not using .iterator()
Pass
test_prefetch_nullable (prefetch_related.tests.NullableTest)
Pass
test_traverse_nullable (prefetch_related.tests.NullableTest)
Pass
test_bug (prefetch_related.tests.Ticket21410Tests)
Pass
test_bug (prefetch_related.tests.Ticket21760Tests)
Pass
test_m2m_then_m2m (prefetch_related.tests.DefaultManagerTests)
Pass
test_order (prefetch_related.tests.LookupOrderingTest)
Pass
test_foreignkey (prefetch_related.tests.ForeignKeyToFieldTest)
Pass
test_m2m (prefetch_related.tests.ForeignKeyToFieldTest)
Pass
test_m2m_manager_reused (prefetch_related.tests.ForeignKeyToFieldTest)
Pass
test_basic (prefetch_related.tests.RawQuerySetTests)
Pass
test_clear (prefetch_related.tests.RawQuerySetTests)
Pass
test_prefetch_before_raw (prefetch_related.tests.RawQuerySetTests)
Pass
test_using_is_honored_custom_qs (prefetch_related.tests.MultiDbTests)
Pass
test_using_is_honored_fkey (prefetch_related.tests.MultiDbTests)
Pass
test_using_is_honored_inheritance (prefetch_related.tests.MultiDbTests)
Pass
test_using_is_honored_m2m (prefetch_related.tests.MultiDbTests)
Pass
test_child_link_prefetch (prefetch_related.tests.MultiTableInheritanceTest)
Pass
test_foreignkey (prefetch_related.tests.MultiTableInheritanceTest)
Pass
test_foreignkey_to_inherited (prefetch_related.tests.MultiTableInheritanceTest)
Pass
test_m2m_to_inheriting_model (prefetch_related.tests.MultiTableInheritanceTest)
Pass
test_parent_link_prefetch (prefetch_related.tests.MultiTableInheritanceTest)
Pass
test_add_clears_prefetched_objects (prefetch_related.tests.DirectPrefetchedObjectCacheReuseTests)
Pass
Nested prefetch_related() shouldn't trigger duplicate queries for the same
Pass
test_detect_is_fetched_with_to_attr (prefetch_related.tests.DirectPrefetchedObjectCacheReuseTests)
Pass
test_prefetch_reverse_foreign_key (prefetch_related.tests.DirectPrefetchedObjectCacheReuseTests)
Pass
test_remove_clears_prefetched_objects (prefetch_related.tests.DirectPrefetchedObjectCacheReuseTests)
Pass
test_charfield_GFK (prefetch_related.tests.GenericRelationTests)
Pass
test_custom_queryset (prefetch_related.tests.GenericRelationTests)
Pass
test_deleted_GFK (prefetch_related.tests.GenericRelationTests)
Pass
test_generic_relation (prefetch_related.tests.GenericRelationTests)
Pass
test_nullable_GFK (prefetch_related.tests.GenericRelationTests)
Pass
test_prefetch_GFK (prefetch_related.tests.GenericRelationTests)
Pass
test_prefetch_GFK_fk_pk (prefetch_related.tests.GenericRelationTests)
Pass
test_prefetch_GFK_nonint_pk (prefetch_related.tests.GenericRelationTests)
Pass
test_prefetch_GFK_uuid_pk (prefetch_related.tests.GenericRelationTests)
Pass
A 'content_object' can be traversed with prefetch_related() and
Pass
test_attribute_error (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_bool (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_clear (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_count (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_exists (prefetch_related.tests.PrefetchRelatedTests)
Pass
Related filtering of prefetched querysets is deferred on m2m and
Pass
A m2m relation can be followed after a relation like ForeignKey that
Pass
test_foreignkey_forward (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_foreignkey_reverse (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_forward_m2m_to_attr_conflict (prefetch_related.tests.PrefetchRelatedTests)
Pass
Objects retrieved with .get() get the prefetch behavior.
Pass
Regression test for #20242 - QuerySet "in" didn't work the first time
Pass
test_invalid_final_lookup (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_len (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_forward (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_prefetching_iterator_with_chunks (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_prefetching_iterator_without_chunks (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_prefetching_iterator_without_chunks_warning (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_reverse (prefetch_related.tests.PrefetchRelatedTests)
Pass
A m2m can be followed through another m2m.
Pass
test_m2m_then_m2m_object_ids (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_then_reverse_fk_object_ids (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_m2m_then_reverse_one_to_one_object_ids (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_named_values_list (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_onetoone_reverse_no_match (prefetch_related.tests.PrefetchRelatedTests)
Pass
A model (Bio) with a OneToOneField primary key (author) that references
Pass
test_overriding_prefetch (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_prefetch_eq (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_reverse_m2m_to_attr_conflict (prefetch_related.tests.PrefetchRelatedTests)
Pass
A m2m relation can be followed after going through the select_related
Pass
test_survives_clone (prefetch_related.tests.PrefetchRelatedTests)
Pass
test_ambiguous (prefetch_related.tests.CustomPrefetchTests)
Pass
test_custom_qs (prefetch_related.tests.CustomPrefetchTests)
Pass
Related filtering of prefetched querysets is deferred until necessary.
Pass
test_generic_rel (prefetch_related.tests.CustomPrefetchTests)
Pass
test_m2m (prefetch_related.tests.CustomPrefetchTests)
Pass
test_m2m_through_fk (prefetch_related.tests.CustomPrefetchTests)
Pass
test_m2m_through_gfk (prefetch_related.tests.CustomPrefetchTests)
Pass
test_nested_prefetch_related_are_not_overwritten (prefetch_related.tests.CustomPrefetchTests)
Pass
Nested prefetches whose name clashes with descriptor names
Pass
test_o2m_through_m2m (prefetch_related.tests.CustomPrefetchTests)
Pass
test_raw_queryset (prefetch_related.tests.CustomPrefetchTests)
Pass
test_reverse_m2m (prefetch_related.tests.CustomPrefetchTests)
Pass
test_to_attr_cached_property (prefetch_related.tests.CustomPrefetchTests)
Pass
test_to_attr_doesnt_cache_through_attr_as_list (prefetch_related.tests.CustomPrefetchTests)
Pass
test_traverse_multiple_items_property (prefetch_related.tests.CustomPrefetchTests)
Pass
test_traverse_qs (prefetch_related.tests.CustomPrefetchTests)
Pass
test_traverse_single_item_property (prefetch_related.tests.CustomPrefetchTests)
Pass
test_values_queryset (prefetch_related.tests.CustomPrefetchTests)
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.