On Sun, Feb 16, 2014 at 6:56 AM, Levi McDonough <levimcdono...@gmail.com> wrote:
>
> Hey everyone, I'm having problems with caching a queryset and storing the
> results of the query in the cache so the queries don't run when the page
> loads.  I am caching and pickling the queryset, however I keep just storing
> the query but not the results of the query.
>
>       I've read through the docs several times and still am not getting what
> I need. I am caching and pickling a query set via a management command which
> should be evaluated by django so the query is not executed on page load, it
> should use cached results at the time of the cache object creation. But the
> debug toolbar is showing that the queries are still running when the page
> loads. So I'm assuming I'm just caching the query and not the results. Below
> is my code, any help would be great, I've been stuck on this for a few days
> now... thanks.
>
> management command to generate the cache key, value:
>
>     images_query = ImageAssociations.objects.filter(place_id =
> place_id).order_by(-image_score')[:10]

This is still an unevaluated queryset - no query has been run, and no
model instances created - so when you pickle it, all that gets pickled
is the query.

If you want to store a list of ten item objects, well... do so :)

     images = pickle.dumps(list(images_query))
     cache.set('images', images, timeout=9999)

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1KZt7yGe2-oywaTnYF6wwGdLbxjU8mk0jAunnCVd3ceQQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to