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

  • SEARCH
  • Home
  • 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
  • 2 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

For example, I have a model like this: Class Doggy(models.Model): name = models.CharField(u'Name', max_length
I am playing around with using Django and a few arduinos. From my models.py
Update: Using Django 1.2.1 and Python 2.5.2 as offered by Dreamhost. I'm having issues
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
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata
I'm having an odd problem while deploying a Django site using Fabric. I've configured
Using Django's built in models, how would one create a triple-join between three models.
using django 1.4 I have a model with a datetimefield. I imported django.utils.timezone to
I'm having trouble pulling comments into my template using django comments. I'd like to

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.