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
urls.py 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,
object_detail

from application.news.models import Article, Category
from application.news.views import news_article_detail

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

(r'^all/$',
object_list, {
'queryset' : Article.live.all(),
}, 'news_all'),

(r'^(?P<slug>[-\w]+)/$',
object_detail, {
'queryset' : Category.objects.all(),
}, 'news_category'),

# the article should appear under the category menu item
(r'^(?P<category_slug>[-\w]+)/(?P<slug>[-\w]+)/$',
news_article_detail, {
'queryset' : Article.live.all(),
}, '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 Article.live.all() 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???

Thanks
Jay

On May 19, 10:36 pm, Kirill Spitsin <t...@0x746e.org.ua> wrote:
> On Wed, May 18, 2011 at 11:45:46PM -0700,JamesHargreaveswrote:
>
> ...
>
> > Firstly, when I query for LIVE objects in my view via
> > Article.live.all() 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 datetime.datetime.now() 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]http://docs.djangoproject.com/en/1.3/topics/db/queries/#caching-and-q...
>
> --
> 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 django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment