Ticket #21760: patch_21760.diff

File patch_21760.diff, 2.6 KB (added by valtron2000@…, 11 years ago)
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 82e7725..92b85a6 100644
    a b class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec  
    310310        instance_attr = self.field.get_local_related_value
    311311        instances_dict = dict((instance_attr(inst), inst) for inst in instances)
    312312        related_field = self.field.foreign_related_fields[0]
    313 
    314         # FIXME: This will need to be revisited when we introduce support for
    315         # composite fields. In the meantime we take this practical approach to
    316         # solve a regression on 1.6 when the reverse manager in hidden
    317         # (related_name ends with a '+'). Refs #21410.
    318         if self.field.rel.is_hidden():
    319             query = {'%s__in' % related_field.name: set(instance_attr(inst)[0] for inst in instances)}
    320         else:
    321             query = {'%s__in' % self.field.related_query_name(): instances}
    322 
    323         qs = self.get_queryset(instance=instances[0]).filter(**query)
     313       
     314        related_pks = set()
     315        for instance in instances:
     316            related_pk = getattr(instance, self.field.attname)
     317            if related_pk is not None:
     318                related_pks.add(related_pk)
     319       
     320        qs = self.get_queryset(instance=instances[0]).filter(pk__in = related_pks)
    324321        # Since we're going to assign directly in the cache,
    325322        # we must manage the reverse relation cache manually.
    326323        if not self.field.rel.multiple:
  • tests/prefetch_related/tests.py

    diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
    index 5dfca0f..bf09ae8 100644
    a b class Ticket21410Tests(TestCase):  
    996996
    997997    def test_bug(self):
    998998        list(Author2.objects.prefetch_related('first_book', 'favorite_books'))
     999
     1000
     1001class Ticket21760Tests(TestCase):
     1002
     1003    def setUp(self):
     1004        for _ in xrange(3):
     1005            house = House.objects.create()
     1006            for _ in xrange(3):
     1007                Room.objects.create(house = house)
     1008
     1009    @override_settings(DEBUG=True)
     1010    def test_bug(self):
     1011        rooms = list(Room.objects.all().prefetch_related('house'))
     1012
     1013        house_ids = {
     1014            r.house_id for r in rooms
     1015            if r.house_id
     1016        }
     1017
     1018        prefetch_query = connection.queries[-1]['sql']
     1019        lookup_id_str = prefetch_query.split('- PARAMS = ')[1]
     1020        lookup_ids = set(map(int, lookup_id_str[1:-1].split(', ')))
     1021
     1022        self.assertEqual(house_ids, lookup_ids)
Back to Top