Hello,

I am using Django. I am having a few issues with caching of QuerySets
for news/category models:

class Category(models.Model):
    title = models.CharField(max_length=60)
    slug = models.SlugField(unique=True)

class PublishedArticlesManager(models.Manager):
    def get_query_set(self):
        return super(PublishedArticlesManager, self).get_query_set() \
                    .filter(published__lte=datetime.datetime.now())

class Article(models.Model):
    category = models.ForeignKey(Category)

    title = models.CharField(max_length=60)
    slug = models.SlugField(unique = True)
    story = models.TextField()
    author = models.CharField(max_length=60, blank=True)
    published = models.DateTimeField(
        help_text=_('Set to a date in the future to publish later.'))
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    live = PublishedArticlesManager()
    objects = models.Manager()
Note - I have removed some fields to save on complexity...

There are a few (related) issues with the above.

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.

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!?

Finally, why is none of this being cached?

Any ideas how to make the correct time be used consistently? Can
someone please explain why the latter setup appears to work?

Thanks Jay

P.S - database queries below, note the date variations.

SELECT LIVE ARTICLES, query #1:

SELECT `news_article`.`id`, `news_article`.`category_id`,
`news_article`.`title`, `news_article`.`slug`,
`news_article`.`teaser`, `news_article`.`summary`,
`news_article`.`story`, `news_article`.`author`,
`news_article`.`published`, `news_article`.`created`,
`news_article`.`updated` FROM `news_article` WHERE
`news_article`.`published` <= '2011-05-17 21:55:41'  ORDER BY
`news_article`.`published` DESC, `news_article`.`slug` ASC;
SELECT LIVE ARTICLES, query #1:

SELECT `news_article`.`id`, `news_article`.`category_id`,
`news_article`.`title`, `news_article`.`slug`,
`news_article`.`teaser`, `news_article`.`summary`,
`news_article`.`story`, `news_article`.`author`,
`news_article`.`published`, `news_article`.`created`,
`news_article`.`updated` FROM `news_article` WHERE
`news_article`.`published` <= '2011-05-17 21:55:41'  ORDER BY
`news_article`.`published` DESC, `news_article`.`slug` ASC;
CATEGORY SELECT ARTICLES, query #1:

SELECT `news_article`.`id`, `news_article`.`category_id`,
`news_article`.`title`, `news_article`.`slug`,
`news_article`.`teaser`, `news_article`.`summary`,
`news_article`.`story`, `news_article`.`author`,
`news_article`.`published`, `news_article`.`created`,
`news_article`.`updated` FROM `news_article` WHERE
(`news_article`.`published` <= '2011-05-18 21:21:33'  AND
`news_article`.`category_id` = 1 ) ORDER BY `news_article`.`published`
DESC, `news_article`.`slug` ASC;
CATEGORY SELECT ARTICLES, query #1:

SELECT `news_article`.`id`, `news_article`.`category_id`,
`news_article`.`title`, `news_article`.`slug`,
`news_article`.`teaser`, `news_article`.`summary`,
`news_article`.`story`, `news_article`.`author`,
`news_article`.`published`, `news_article`.`created`,
`news_article`.`updated` FROM `news_article` WHERE
(`news_article`.`published` <= '2011-05-18 21:26:06'  AND
`news_article`.`category_id` = 1 ) ORDER BY `news_article`.`published`
DESC, `news_article`.`slug` ASC;

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to