Ticket #18309: prefetch_related_multi_table_bug.diff

File prefetch_related_multi_table_bug.diff, 4.0 KB (added by milosu, 12 years ago)
  • django_versions/trunk/tests/modeltests/prefetch_related/tests.py

     
    77
    88from .models import (Author, Book, Reader, Qualification, Teacher, Department,
    99    TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
    10     BookWithYear, Person, House, Room, Employee)
     10    BookWithYear, BookReview, Person, House, Room, Employee)
    1111
    1212
    1313class PrefetchRelatedTests(TestCase):
     
    327327        self.authorAddress = AuthorAddress.objects.create(
    328328            author=self.author1, address='SomeStreet 1')
    329329        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")
    330334
    331335    def test_foreignkey(self):
    332336        with self.assertNumQueries(2):
     
    335339                         for obj in qs]
    336340        self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []])
    337341
     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
    338347    def test_m2m_to_inheriting_model(self):
    339348        qs = AuthorWithAge.objects.prefetch_related('books_with_year')
    340349        with self.assertNumQueries(2):
  • django_versions/trunk/tests/modeltests/prefetch_related/models.py

     
    6868    class Meta:
    6969        ordering = ['id']
    7070
     71class BookReview(models.Model):
     72    book = models.ForeignKey(BookWithYear)
     73    notes = models.TextField(null=True, blank=True)
    7174
    7275## Models for default manager tests
    7376
  • django_versions/trunk/django/db/models/fields/related.py

     
    339339            params = {'%s__pk__in' % self.field.rel.field_name: vals}
    340340        else:
    341341            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'
    342345        return (self.get_query_set(instance=instances[0]).filter(**params),
    343                 attrgetter(self.field.rel.field_name),
     346                attrgetter(field_attr),
    344347                attrgetter(self.field.attname),
    345348                True,
    346349                self.cache_name)
     
    360363                    params = {'%s__pk' % self.field.rel.field_name: val}
    361364                else:
    362365                    params = {'%s__exact' % self.field.rel.field_name: val}
    363                 qs = self.get_query_set(instance=instance)
    364                 # Assuming the database enforces foreign keys, this won't fail.
    365                 rel_obj = qs.get(**params)
     366                # One2one fields with multi table inheritance
     367                # are fetched using implicit select_related
     368                # instance is equal to rel_obj in this case
     369                if self.field.rel.parent_link:
     370                    rel_obj = instance
     371                else:
     372                    qs = self.get_query_set(instance=instance)
     373                    # Assuming the database enforces foreign keys, this won't fail.
     374                    rel_obj = qs.get(**params)
    366375                if not self.field.rel.multiple:
    367376                    setattr(rel_obj, self.field.related.get_cache_name(), instance)
    368377            setattr(instance, self.cache_name, rel_obj)
Back to Top