title: Pickle a django query?
body: <p>Is it possible to pickle or somehow store a django query in the database? This won`t work :</p>
  
  <pre><code>u = User.objects.all 
  import cPickle
  pickled_query = cPickle.dumps(u) # and store the pickled_query in a db-field.
  </code></pre>
  
  <p>Any thoughts?</p>
  
  <p>Updated:</p>
  
  <pre><code>import cPickle
  
  class CustomData(models.Model):
      name = models.CharField(max_length = 30)
      pickled_query = models.CharField(max_length = 300)
  
  def get_custom_result(self):
      q = cPickle.loads(self.pickled_query)
      return q()
  
  &gt;&gt;&gt; cd = CustomData(name="My data", pickled_query=cPickle.dumps(User.objects.all))
  &gt;&gt;&gt; cd.save()
  &gt;&gt;&gt; for item in cd.get_custom_result(): print item
  # prints all the users in the database, not printing the query result 
  # when pickled, but when called like cd.get_custom_result(), that is when 
  # the query is actually executed.
  </code></pre>
  
  <p>Now that's what I want to do. I know this actual piece of code won`t run, but it shows my intention; to store a query, not the result, and execute that query at some point in the future.</p>
  
  <p>Update #2:</p>
  
  <pre><code>&gt;&gt;&gt; from django.contrib.auth.models import User
  # adds two users; super and test
  &gt;&gt;&gt; u = User.objects.filter(username = 'test')
  &gt;&gt;&gt; import cPickle
  &gt;&gt;&gt; s = cPickle.dumps(u.query)
  &gt;&gt;&gt; s2 = cPickle.loads(s)
  &gt;&gt;&gt; o = s2.model.objects.all()
  &gt;&gt;&gt; o.query = s2
  &gt;&gt;&gt; for i in o: print i
  ...
  super
  </code></pre>
  
  <p>Still not completly satisfied. I know QuerySets are lazy so doing s2.model.objects.all() won't execute a query fetching all users? </p>