Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:09+00:00 2026-05-22T19:52:09+00:00

I am using Django. I am having a few issues with caching of QuerySets

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:52:10+00:00Added an answer on May 22, 2026 at 7:52 pm

    I have now fixed this issue. It appears the problem was that the queryset returned by Article.live.all() was being cached in my urls.py! I was using function-based generic-views:

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

    I have now changed this to use the class-based approach, as advised in the latest Django documentation:

    url(r'^all/$', ListView.as_view(
            model=Article,
        ), name="news_all"),
    

    This now works as expected – by specifying the model attribute rather than the queryset attribute the query is QuerySet is created at compile-time instead of runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having issues with get_absolute_url in my django templates when using django page
I am having problems using django-tagging . I try to follow the documentation but
Using Django's built in models, how would one create a triple-join between three models.
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata
I'm using django-compress to shrink my JavaScript files. However, I am now having trouble
I am using templates with django. I am having a problem where the Context
I have started using django-piston for creating APIS but I am having trouble finding
I'm having a bit of trouble finding an answer on this: Using Django forms,
I'm using Django 1.2, and am having trouble generating good XML for a podcast.
I'm having some difficulty getting my django tests to run properly; I'm using nose,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.