Ticket #18309: prefetch_related_multi_table_bug.2.diff
File prefetch_related_multi_table_bug.2.diff, 3.0 KB (added by , 12 years ago) |
---|
-
django_versions/trunk/tests/modeltests/prefetch_related/tests.py
7 7 8 8 from .models import (Author, Book, Reader, Qualification, Teacher, Department, 9 9 TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge, 10 BookWithYear, Person, House, Room, Employee)10 BookWithYear, BookReview, Person, House, Room, Employee) 11 11 12 12 13 13 class PrefetchRelatedTests(TestCase): … … 327 327 self.authorAddress = AuthorAddress.objects.create( 328 328 author=self.author1, address='SomeStreet 1') 329 329 self.book2.aged_authors.add(self.author2, self.author3) 330 self.br1 = BookReview.objects.create( 331 book = self.book1, notes = "review book1") 332 self.br2 = BookReview.objects.create( 333 book = self.book2, notes = "review book2") 330 334 331 335 def test_foreignkey(self): 332 336 with self.assertNumQueries(2): … … 335 339 for obj in qs] 336 340 self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []]) 337 341 342 with self.assertNumQueries(2): 343 qs = BookReview.objects.prefetch_related('book') 344 reviews = [obj.book.title for obj in qs] 345 self.assertEquals(reviews, ["Poems", "More poems"]) 346 338 347 def test_m2m_to_inheriting_model(self): 339 348 qs = AuthorWithAge.objects.prefetch_related('books_with_year') 340 349 with self.assertNumQueries(2): -
django_versions/trunk/tests/modeltests/prefetch_related/models.py
68 68 class Meta: 69 69 ordering = ['id'] 70 70 71 class BookReview(models.Model): 72 book = models.ForeignKey(BookWithYear) 73 notes = models.TextField(null=True, blank=True) 71 74 72 75 ## Models for default manager tests 73 76 -
django_versions/trunk/django/db/models/fields/related.py
339 339 params = {'%s__pk__in' % self.field.rel.field_name: vals} 340 340 else: 341 341 params = {'%s__in' % self.field.rel.field_name: vals} 342 field_attr = self.field.rel.field_name 343 if other_field.rel and other_field.rel.parent_link: 344 field_attr += '_id' 342 345 return (self.get_query_set(instance=instances[0]).filter(**params), 343 attrgetter( self.field.rel.field_name),346 attrgetter(field_attr), 344 347 attrgetter(self.field.attname), 345 348 True, 346 349 self.cache_name)