Fixed #26264 -- Fixed prefetch_related() crashes with values_list(flat=True)
This commit is contained in:
parent
3389c5ea22
commit
5e2c4d7afb
|
@ -1421,11 +1421,12 @@ def prefetch_related_objects(model_instances, *related_lookups):
|
||||||
if not hasattr(obj, '_prefetched_objects_cache'):
|
if not hasattr(obj, '_prefetched_objects_cache'):
|
||||||
try:
|
try:
|
||||||
obj._prefetched_objects_cache = {}
|
obj._prefetched_objects_cache = {}
|
||||||
except AttributeError:
|
except (AttributeError, TypeError):
|
||||||
# Must be in a QuerySet subclass that is not returning
|
# Must be an immutable object from
|
||||||
# Model instances, either in Django or 3rd
|
# values_list(flat=True), for example (TypeError) or
|
||||||
# party. prefetch_related() doesn't make sense, so quit
|
# a QuerySet subclass that isn't returning Model
|
||||||
# now.
|
# instances (AttributeError), either in Django or a 3rd
|
||||||
|
# party. prefetch_related() doesn't make sense, so quit.
|
||||||
good_objects = False
|
good_objects = False
|
||||||
break
|
break
|
||||||
if not good_objects:
|
if not good_objects:
|
||||||
|
|
|
@ -44,6 +44,17 @@ class UUIDPrefetchRelated(TestCase):
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0):
|
||||||
self.assertEqual(2, len(flea.pets_visited.all()))
|
self.assertEqual(2, len(flea.pets_visited.all()))
|
||||||
|
|
||||||
|
def test_prefetch_related_from_uuid_model_to_uuid_model_with_values_flat(self):
|
||||||
|
pet = Pet.objects.create(name='Fifi')
|
||||||
|
pet.people.add(
|
||||||
|
Person.objects.create(name='Ellen'),
|
||||||
|
Person.objects.create(name='George'),
|
||||||
|
)
|
||||||
|
self.assertSequenceEqual(
|
||||||
|
Pet.objects.prefetch_related('fleas_hosted').values_list('id', flat=True),
|
||||||
|
[pet.id]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class UUIDPrefetchRelatedLookups(TestCase):
|
class UUIDPrefetchRelatedLookups(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue