Monday, May 30, 2011

Re: Django model manager "live" object issues

Hi Kirill,

Thanks for your response and sorry for my tardiness in replying!

I think you are right - my queryset is cached in the view, as my looks like this:

from django.conf.urls.defaults import *
from django.views.generic.simple import redirect_to
from django.views.generic.list_detail import object_list,

from import Article, Category
from import news_article_detail

urlpatterns = patterns('',
redirect_to, { 'url' : 'all' }),

object_list, {
'queryset' :,
}, 'news_all'),

object_detail, {
'queryset' : Category.objects.all(),
}, 'news_category'),

# the article should appear under the category menu item
news_article_detail, {
'queryset' :,
}, 'news_article'),

Note - I realise this is using function-based generic-views and I am
going to change this to class-based shortly, in the hope that this
will fix the issue.

Anyway, so the call to will create a QuerySet and
this is stored in the queryset attribute for the "news_all" and
"news_article" views. I don't know how to create my view so that the
filter is done at runtime instead of compile time. I could of course
do the check in my view code, but that's not great programming
practice and also bad from a performance perspective.

Any thoughts???


On May 19, 10:36 pm, Kirill Spitsin <> wrote:
> On Wed, May 18, 2011 at 11:45:46PM -0700,JamesHargreaveswrote:
> ...
> > Firstly, when I query for LIVE objects in my view via
> > if I refresh the page repeatedly I can see (in
> > MYSQL logs) the same database query being made with exactly the same
> > date in the where clause - ie - the is being
> > evaluated at compile time rather than runtime. I need the date to be
> > evaluated at runtime.
> I can't reproduce such behavior.  `.get_query_set()` is evaluted when
> queryset is returned from manager, so, maybe, you cache queryset
> somewhere in your view?
> > Secondly, when I use the articles_set method on the Category object
> > this appears to work correctly - the datetime used in the query
> > changes each time the query is run - again I can see this in the logs.
> > However, I am not quite sure why this works, since I don't have
> > anything in my code to say that the articles_set query should return
> > LIVE entries only!?
> The first manager defined on model is interepted as "default manager".
> You probably want to put line with `live` manager after `objects`
> manager in `Article` declaration.
> > Finally, why is none of this being cached?
> Not quite so, QuerySet has a cache [1].
> .. [1]
> --
> Kirill Spitsin

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment