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 7934729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:16+00:00 2026-06-03T21:43:16+00:00

I’m trying to expire a view-level cache on a model’s post_save (that was set

  • 0

I’m trying to expire a view-level cache on a model’s post_save (that was set via https://docs.djangoproject.com/en/1.3/topics/cache/?from=olddocs#the-per-view-cache). I did some googling and found this answer here on SO: Expire a view-cache in Django? but it’s not working for me.

I asked around in the #django room on freenode and the consensus was that this was probably due to the recent caching changes made in 1.3

Does anyone have any idea on how I can wipe out the cache entry for a model keyed off it’s get_absolute_url()?

  • 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-06-03T21:43:18+00:00Added an answer on June 3, 2026 at 9:43 pm

    I figured it out!

    Cheers to ilvar for pointing me in the right direction. My implementation is below. I created a property named cache_key and added a post_save receiver onto the sub class of the models whose view-level caches I needed to clear after they had been updated. Suggestions for improvement are always welcome!

    from django.conf import settings
    from django.core.cache import cache
    from django.db.models.signals import post_save
    from django.http import HttpRequest
    from django.utils.cache import _generate_cache_header_key
    
    from someapp.models import BaseModelofThisClass
    
    class SomeModel(BaseModelofThisClass):
        ...
        @property
        def cache_key(self):
            # Create a fake request object
            request = HttpRequest()
            # Set the request method
            request.method = "GET"
            # Set the request path to be this object's permalink.
            request.path = self.get_absolute_url()
            # Grab the key prefix (if it exists) from settings
            key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
            # Generate this object's cache key using django's key generator
            key = _generate_cache_header_key(key_prefix, request)
            # This is a bit hacky but necessary as I don't know how to do it
            # properly otherwise. While generating a cache header key, django
            # uses the language of the HttpRequest() object as part of the
            # string. The fake request object, by default, uses
            # settings.LANGUAGE_CODE as it's language (in my case, 'en-us')
            # while the true request objects that are used in building views
            # use settings.LANGUAGES ('en'). Instead of replacing the segment
            # of the string after the fact it would be far better create a more
            # closely mirrored HttpRequest() object prior to passing it to
            # _generate_cache_header_key().
            key = key.replace(settings.LANGUAGE_CODE, settings.LANGUAGES[settings.DEFAULT_LANGUAGE][0])
    
            return key
    
        @receiver(post_save)
        def clear_cache_for_this_item(sender, instance, **kwargs):
            # If this is a sub-class of another model
            if sender not in BaseModelofThisClass.__subclasses__():
                return
            else:
                cache.delete(instance.cache_key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.