Salt a django question?

Is it possible to saw or somehow store a django request in a database? This will not work:

u = User.objects.all import cPickle pickled_query = cPickle.dumps(u) # and store the pickled_query in a db-field. 

Any thoughts?

Updated:

 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() >>> cd = CustomData(name="My data", pickled_query=cPickle.dumps(User.objects.all)) >>> cd.save() >>> 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. 

Now what I want to do. I know that this actual piece of code did not run, but it shows my intention; to save the query, not the result, and execute that query at some point in the future.

Update # 2:

 >>> from django.contrib.auth.models import User # adds two users; super and test >>> u = User.objects.filter(username = 'test') >>> import cPickle >>> s = cPickle.dumps(u.query) >>> s2 = cPickle.loads(s) >>> o = s2.model.objects.all() >>> o.query = s2 >>> for i in o: print i ... super 

Still not done. I know QuerySets are lazy, so s2.model.objects.all () will not execute a query that gets all users?

+4
source share
1 answer

Here is the documentation . Basically, comb the query attribute if you want to recreate the SQL query, and pickle the entire set of queries if you want to paint a snapshot of the current results.

Your example will be the last if you select the result of all() instead of the bound method.

+5
source

All Articles