queryset caching - without caching middleware

83 views
Skip to first unread message

Henrik Genssen

unread,
Jul 5, 2012, 7:43:06 AM7/5/12
to django...@googlegroups.com
Hi all,

what's the magic here?

I am using django 1.3 with sqlite or postgres by invoking:
runserver 10.150.2.15:8080 --noreload

I have a view (no cache decorator) doing simple querys on the orm and return that as html - nothing fancy
The view is executed on each request, but the database is hit only on the first request
After that devserver shows only one query each request querying for django_session

newly data is not fetched...

any idea?

my middleware looks like this:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'schiwago.middleware.header.ResponseInjectHeader',
'schiwago.middleware.auth.BasicAuthMiddleware',
'django.middleware.transaction.TransactionMiddleware',
)

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'schiwago',
'transdb',
'south',
'mediafactory.ui',
'relatorio',
'djcelery',
'devserver'
)


regards

Henrik

Henrik Genssen

h...@miadi.net
Tel. +49 (0)451/6195650
Fax. +49 (0)451/6195655
http://www.miadi.net
http://www.facebook.com/pages/Miadi/150192061701844

miadi GmbH
Gesch�ftsf�hrer: Henrik Genssen
Sitz der Gesellschaft: H�xstra�e 1 - 9, 23552 L�beck
Amtsgericht L�beck HRB 10223, USt-IdNr DE
Lieferungen und Leistungen erfolgen ausschlie�lich auf Grundlage unserer allgemeinen Gesch�ftsbedingungen

hinnack

unread,
Jul 5, 2012, 8:28:56 AM7/5/12
to django...@googlegroups.com
hmm, Ok, Queryset Caching happens alle the time...

So here are some more questions on this:
- where is the data stored?
- can I ask a model, if its result is cached or a fresh one?
- can I disable or force a "refresh" of the cache?
- how would i use a query in a property of a class, without running into the cache phenomena?

regards

Henrik

Tom Evans

unread,
Jul 5, 2012, 10:31:08 AM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 1:28 PM, hinnack <henrik....@miadi.net> wrote:
> hmm, Ok, Queryset Caching happens alle the time...
>
> So here are some more questions on this:
> - where is the data stored?
> - can I ask a model, if its result is cached or a fresh one?
> - can I disable or force a "refresh" of the cache?
> - how would i use a query in a property of a class, without running into the
> cache phenomena?
>
> regards
>
> Henrik
>

Could you show where and how you are executing the query? If the
queryset is a global, and does not go out of scope at the end of the
request, then reusing the queryset will not cause the queryset to be
re-evaluated.

Eg, in this example, categories is a global outside of the view, and
once evaluated, will not be re-evaluated just because you used it in a
view.

categories = Category.objects.all()

def index(request):
return render_to_response('index.html': { 'categories': categories })

When querysets are executed is documented here:

https://docs.djangoproject.com/en/1.4/ref/models/querysets/#when-querysets-are-evaluated

Cheers

Tom

Melvyn Sopacua

unread,
Jul 5, 2012, 10:49:03 AM7/5/12
to django...@googlegroups.com
On 5-7-2012 14:28, hinnack wrote:
> - can I disable or force a "refresh" of the cache?

<http://stackoverflow.com/questions/3346124/how-do-i-force-django-to-ignore-any-caches-and-reload-data>

--
Melvyn Sopacua


Tom Evans

unread,
Jul 5, 2012, 10:55:39 AM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 3:49 PM, Melvyn Sopacua <m.r.s...@gmail.com> wrote:
> On 5-7-2012 14:28, hinnack wrote:
>> - can I disable or force a "refresh" of the cache?
>
> <http://stackoverflow.com/questions/3346124/how-do-i-force-django-to-ignore-any-caches-and-reload-data>
>

Transactions/READ REPEATED behaviour is unlikely to be affecting him
across requests, since the DB connection is opened and closed at the
start and end of each request.

Cheers

Tom

Henrik Genssen

unread,
Jul 5, 2012, 11:08:51 AM7/5/12
to django...@googlegroups.com
>Could you show where and how you are executing the query? If the
>queryset is a global, and does not go out of scope at the end of the
>request, then reusing the queryset will not cause the queryset to be
>re-evaluated.
>
>Eg, in this example, categories is a global outside of the view, and
>once evaluated, will not be re-evaluated just because you used it in a
>view.
>
>categories = Category.objects.all()
>
>def index(request):
> return render_to_response('index.html': { 'categories': categories })
>
>When querysets are executed is documented here:
>
>https://docs.djangoproject.com/en/1.4/ref/models/querysets/#when-querysets-are-evaluated


yep, global caught me.
I have a class:

class table(object):
queryset = Category.objects.all()
column1 = ...
column2 = ...

def render(self):
return '<table>...'

in a view I am doing:

tbl = table()
# somtimes adding filters
tbl.queryset = tbl.queryset.filter(...)


I want to create a html table like "forms" in django

Is it enough to call the all() method on the queryset in my rendering method to get the queryset query the db again?

regards

Henrik

Tom Evans

unread,
Jul 5, 2012, 11:28:33 AM7/5/12
to django...@googlegroups.com
Yes, so that queryset is created once, when the class definition is
parsed, and then populated once. Regardless of how many instances of
table that you create, they all get the same object, created when the
class was parsed.

The simplest solution would be to simply assign that queryset inside
the init method, instead of making it a class member, eg:

class table(object):
column1 = ...
column2 = ...
def __init__(self):
self.queryset = Category.objects.all()

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages