views:

305

answers:

4

I have problem storing a big queryset in the session. This queryset is from a search and I need to store it for paginate inside every result. This is the code in my view:

c = queryset.order_by('-order') 
request.session['query_search'] = c

You can see an example in my site: http://www.lukmi.com/classifieds/chicas/escorts/barcelona/ This is a list of results(the queryset) and I store it in the session because I need to use it in each profile to go to next profile.

I have some problems to store it because is very big. Anybody knows a good solution?

A: 

You could store only the list of PKs and then query through them when you need to.

Ignacio Vazquez-Abrams
A: 

Or may you can store the sql generated by the query and then execute.

request.session['query_search'] = c.query.as_sql()
diegueus9
The problem with doing it this way is that the query results may change from page to page.
Ignacio Vazquez-Abrams
A: 

Thanks a lot for answer. I think store the sql is a good solution. How can I execute the sql statement? I never work with sql directly only I only work with the models.

cisco
A: 

I'm currently developing a very similar site to yours also in Django. I stored the queryset in cache, where cache key is an urlencoded string of search parameters- this way if someone performs identical search it doesn't have to repeat the expensive queries to get the same results.

In your case, you can generate parameters list from url.

 form = form_class(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        persons = .... #expensive queries that fetch the results of search

        cache_id = urlencode(cd.items())
        #create md5 hash to use in link to results
        cache_id = hashlib.md5(cache_id).hexdigest()
        cache.set(cache_id, persons, CACHE_TIMEOUT)

        #also store form data in cache, so the form can be easily reconstructed from cache id
        cache.set(cache_id+'_form', request.POST, CACHE_TIMEOUT)
fest