Ticket #18309: prefetch_related_multi_table_bug.2.diff

File prefetch_related_multi_table_bug.2.diff, 3.0 KB (added by milosu, 12 years ago)

shorter and still working patch, which does not lead to deadlock while resolving pkey

  • 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)
Back to Top