mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #17838 - prefetch_related fails for GenericForeignKeys when related object id is not a CharField/TextField
Thanks to mkai for the report and debugging, and tmitchell and Przemek Lewandowski for their work on the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17744 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -125,6 +125,15 @@ class Bookmark(models.Model):
|
||||
tags = generic.GenericRelation(TaggedItem)
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
comment = models.TextField()
|
||||
|
||||
# Content-object field
|
||||
content_type = models.ForeignKey(ContentType)
|
||||
object_pk = models.TextField()
|
||||
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
|
||||
|
||||
|
||||
## Models for lookup ordering tests
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from django.test import TestCase
|
||||
|
||||
from .models import (Author, Book, Reader, Qualification, Teacher, Department,
|
||||
TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
|
||||
BookWithYear, Person, House, Room, Employee)
|
||||
BookWithYear, Person, House, Room, Employee, Comment)
|
||||
|
||||
|
||||
class PrefetchRelatedTests(TestCase):
|
||||
@@ -254,6 +254,14 @@ class GenericRelationTests(TestCase):
|
||||
qs = TaggedItem.objects.prefetch_related('content_object')
|
||||
list(qs)
|
||||
|
||||
def test_prefetch_GFK_nonint_pk(self):
|
||||
Comment.objects.create(comment="awesome", content_object=self.book1)
|
||||
|
||||
# 1 for Comment table, 1 for Book table
|
||||
with self.assertNumQueries(2):
|
||||
qs = Comment.objects.prefetch_related('content_object')
|
||||
[c.content_object for c in qs]
|
||||
|
||||
def test_traverse_GFK(self):
|
||||
"""
|
||||
Test that we can traverse a 'content_object' with prefetch_related() and
|
||||
|
||||
Reference in New Issue
Block a user