﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27159	Pickling query with an __in=inner_qs lookup causes evaluation evaluation of inner_qs	Jani Tiainen	Jani Tiainen	"According to documentation pickling queryset.query should be correct approach and it should only pickle only relevant information to recreate queryset.query again. 

Given models:
{{{#!python
from django.db import models

# Create your models here.
class ParentModel(models.Model):
    name = models.CharField(max_length=64)

class ChildModel(models.Model):
    name = models.CharField(max_length=64)

    parent = models.ForeignKey('ParentModel', related_name='children')
}}}

Following querys, when pickled causes unwanted evaluation and creating quite large pickled result:
{{{#!python
for x in range(1,10000):
    ParentModel(name='Parent {}'.format(x), ).save()

ChildModel(name='Child 1', parent=ParentModel.objects.all()[0]).save()

parents_1 = ParentModel.objects.all().values_list('pk', flat=True)
children_1 = ChildModel.objects.filter(parent__in=parents_1)

pickled_stuff_1 = pickle.dumps(children_1.query)

parents_2 = ParentModel.objects.all()
children_2 = ChildModel.objects.filter(parent__in=parents_2)

pickled_stuff_2 = pickle.dumps(children_2.query)

# First len is about 74 kilobytes, second len is about 2 megabytes.
print len(pickled_stuff_1), len(pickled_stuff_2)
}}}

When comparing sizes of pickled queries both are relatively big. When inspecting latter pickle it can be seen that it actually contains all instances from fully evaluated queryset. Same behavior exists in 1.8 and 1.10 as well.
"	Bug	closed	Database layer (models, ORM)	1.10	Normal	fixed			Accepted	1	0	0	0	0	0
