Ticket #27159: ticket_27159.diff

File ticket_27159.diff, 2.4 KB (added by DavidFozo, 8 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index f69c775..9e18a3a 100644
    a b class QuerySet(object):  
    204204        """
    205205        Allows the QuerySet to be pickled.
    206206        """
    207         # Force the cache to be fully populated.
    208         self._fetch_all()
    209207        obj_dict = self.__dict__.copy()
    210208        obj_dict[DJANGO_VERSION_PICKLE_KEY] = get_version()
    211209        return obj_dict
  • tests/queryset_pickle/models.py

    diff --git a/tests/queryset_pickle/models.py b/tests/queryset_pickle/models.py
    index 5e60963..da3896c 100644
    a b class Container(object):  
    6060
    6161class M2MModel(models.Model):
    6262    groups = models.ManyToManyField(Group)
     63
     64
     65class Parent(models.Model):
     66    name = models.CharField(max_length=64)
     67
     68
     69class Child(models.Model):
     70    name = models.CharField(max_length=64)
     71    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
     72 No newline at end of file
  • tests/queryset_pickle/tests.py

    diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py
    index 1111a7f..27af90b 100644
    a b from django.test import TestCase  
    99from django.utils import six
    1010from django.utils.version import get_version
    1111
    12 from .models import Container, Event, Group, Happening, M2MModel
     12from .models import Container, Event, Group, Happening, M2MModel, Parent, Child
    1313
    1414
    1515class PickleabilityTestCase(TestCase):
    class PickleabilityTestCase(TestCase):  
    153153        msg = "Pickled queryset instance's Django version 1.0 does not match the current version %s." % get_version()
    154154        with self.assertRaisesMessage(RuntimeWarning, msg):
    155155            pickle.loads(pickle.dumps(qs))
     156
     157    def test_queryset_evaluation(self):
     158        """
     159        #27159 -- Test that no unintentional queryset evaluation happens
     160        """
     161        for i in range(1, 100000):
     162            Parent(name='Parent {}'.format(i)).save()
     163
     164        child = Child(name='Child 1', parent=Parent.objects.all()[0])
     165       
     166        parents = Parent.objects.all()
     167        children = Child.objects.filter(parent__in=parents)
     168
     169        dumped = pickle.dumps(children.query)
     170        # Assume that such a simple query is less than a megabyte pickled
     171        self.assertLess(len(dumped), 1024 * 1024)
     172 No newline at end of file
Back to Top