﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35434	prefetch_related_objects fails to cache UUID FKs when the string representation of a UUID is used	Selcuk Ayguney		"Consider the following models:

{{{
class Pet(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=20)


class Toy(models.Model):
    pet = models.ForeignKey(Pet, models.CASCADE, related_name=""toys"")
    name = models.CharField(max_length=20)
}}}
The following works and caches the deferred {{{pet}}} instance as expected:
{{{
pet = Pet.objects.create(name=""Fifi"")
toy_bone = Toy(pet_id=str(pet.id), name=""Bone"")
print(toy_bone.pet)
}}}
It fails if {{{prefetch_related_objects}}} is used:
{{{
pet = Pet.objects.create(name=""Fifi"")
toy_bone = Toy(pet_id=str(pet.id), name=""Bone"")
prefetch_related_objects([toy_bone], ""pet"")
print(toy_bone.pet)
}}}
{{{
    raise self.RelatedObjectDoesNotExist(
    ^^^^^^^^^^^^^^^^^
prefetch_related.models.Toy.pet.RelatedObjectDoesNotExist: Toy has no pet.
}}}
The behaviour is inconsistent as str to UUID conversion is automatically performed in the first example.

One may argue that the use of {{{prefetch_related_objects}}} is redundant in this case. However, it is useful in async code where deferred lookups are not automatic.

Unit test to reproduce the issue and the suggested fix:
https://github.com/django/django/pull/18132"	Cleanup/optimization	closed	Documentation	dev	Normal	duplicate		Selcuk Ayguney Simon Charette	Unreviewed	1	0	0	0	0	0
