Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion django/db/models/fields/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,10 @@ def get_prefetch_queryset(self, instances, queryset=None):
getattr(result, '_prefetch_related_val_%s' % f.attname)
for f in fk.local_related_fields
),
lambda inst: tuple(getattr(inst, f.attname) for f in fk.foreign_related_fields),
lambda inst: tuple(
f.get_db_prep_value(getattr(inst, f.attname), connection)
for f in fk.foreign_related_fields
),
False,
self.prefetch_cache_name,
)
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.8.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ Bugfixes

* Fixed recording of applied status for squashed (replacement) migrations
(:ticket:`24628`).

* Fixed prefetch_related for models using UUID primary keys on non-PostgreSQL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These release notes should be in 1.9.txt, whether the fix is backported or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't usually document bug fixes in major release notes, is there something else you were thinking about?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I may be wrong here, not sure. I'm sure @timgraham the docs master will know.

databases (:ticket:`24912`).
9 changes: 9 additions & 0 deletions tests/prefetch_related/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from django.contrib.contenttypes.fields import (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're missing a newline between stdlib and django imports.

GenericForeignKey, GenericRelation,
)
Expand Down Expand Up @@ -257,3 +258,11 @@ def __str__(self):

class Meta:
ordering = ['id']


# Model for many-to-many with UUID pk test:

class Pet(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=20)
people = models.ManyToManyField(Person, related_name='pets')
19 changes: 18 additions & 1 deletion tests/prefetch_related/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .models import (
Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book, Bookmark,
BookReview, BookWithYear, Comment, Department, Employee, FavoriteAuthors,
House, LessonEntry, Person, Qualification, Reader, Room, TaggedItem,
House, LessonEntry, Person, Pet, Qualification, Reader, Room, TaggedItem,
Teacher, WordEntry,
)

Expand Down Expand Up @@ -1161,3 +1161,20 @@ def test_bug(self):
prefetcher = get_prefetcher(self.rooms[0], 'house')[0]
queryset = prefetcher.get_prefetch_queryset(list(Room.objects.all()))[0]
self.assertNotIn(' JOIN ', force_text(queryset.query))


class UUIDPrefetchRelated(TestCase):
def setUp(self):
self.pet = Pet.objects.create(name='Fifi')
self.pet.people.add(
*[
Person.objects.create(name='Ellen'),
Person.objects.create(name='George'),
]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have a to build a list here. The following should do.

self.pet.people.add(
    Person.objects.create(name='Ellen'),
    Person.objects.create(name='George'),
)


def test_prefetch_related(self):
with self.assertNumQueries(2):
pet = Pet.objects.filter(pk=self.pet.pk).prefetch_related('people').first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simply this to:

pet = Pet.objects.prefetch_related('people').get(pk=self.pet.pk)

with self.assertNumQueries(0):
self.assertEqual(2, len(pet.people.all()))