Optimizing my Django app on Google App Engine

2019-08-14 04:01发布

I'm optimising a particular use case of my Django app. My first step was to replace a QuerySet with a ValuesQuerySet. This worked quite well but I want to do more. Now I'm considering using Memcache (the app is running on Google App Engine). So my plan is to basically put the ValuesQuerySet in Memcache. However it is my understanding that ValueQuerySet is basically a not yet materialised data structure. In order for the cache to work, the ValueQuerySet needs to materialise first and then put in the Memcache.

According to the Django docs:

list(). Force evaluation of a QuerySet by calling list() on it.

and:

Finally, note a ValuesQuerySet is a subclass of QuerySet, so it has all methods of QuerySet.

But when I try my_values_qs.list() it throws an exception:

AttributeError: 'ValuesQuerySet' object has no attribute 'list'

So, although ValuesQuerySet is a subclass of QuerySet it apparently cannot list() its contents. Which, if true, would mean that the Django docs are wrong or at least misleading.

Am I missing something or are the docs indeed wrong? What would be the best way to materialise the result of ValueQuerySet so that I can store it in Memcache?

1条回答
疯言疯语
2楼-- · 2019-08-14 04:36

You're misreading that bit from the docs. It doesn't say "call the queryset.list() method": it says "call list() on it". In other words, call list(my_queryset), not my_queryset.list() - and in fact that is explicitly illustrated with an example immediately afterwards.

Note that this has nothing to do with subclassing: QuerySet doesn't have a list() method either.

查看更多
登录 后发表回答