Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Invalidate the appropriate SITE_CACHE entry when saving a Site model.

This avoids the problem of, for example, saving a change to the Site model in
the admin interface and then seeing the wrong instanec returned in the next
call to get_current_site().

It's still possible to end up with an inconsistent cache if update() is used to
change the Site model, but that's pretty unavoidable. It's also a slightly odd
way to update a Site model, so if you really need to do that, you can manage to
call SiteManager.clear() at the same time.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 1306079 commit 0dd4ec654f4b22854df841d9ea8f463fdbdf99c3 @malcolmt malcolmt committed
Showing with 22 additions and 3 deletions.
  1. +8 −2 django/contrib/sites/
  2. +14 −1 django/contrib/sites/
10 django/contrib/sites/
@@ -41,12 +41,18 @@ class Meta:
def __unicode__(self):
return self.domain
+ def save(self, *args, **kwargs):
+ super(Site, self).save(*args, **kwargs)
+ # Cached information will likely be incorrect now.
+ if in SITE_CACHE:
+ del SITE_CACHE[]
def delete(self):
pk =
super(Site, self).delete()
- del(SITE_CACHE[pk])
+ del SITE_CACHE[pk]
except KeyError:
15 django/contrib/sites/
@@ -3,7 +3,7 @@
>>> from django.conf import settings
>>> Site(id=settings.SITE_ID, domain="", name="").save()
->>> # Make sure that get_current() does not return a deleted Site object.
+# Make sure that get_current() does not return a deleted Site object.
>>> s = Site.objects.get_current()
>>> isinstance(s, Site)
@@ -13,4 +13,17 @@
Traceback (most recent call last):
DoesNotExist: Site matching query does not exist.
+# After updating a Site object (e.g. via the admin), we shouldn't return a
+# bogus value from the SITE_CACHE.
+>>> _ = Site.objects.create(id=settings.SITE_ID, domain="", name="")
+>>> site = Site.objects.get_current()
+>>> s2 = Site.objects.get(id=settings.SITE_ID)
+>>> = "Example site"
+>>> site = Site.objects.get_current()
+u"Example site"

0 comments on commit 0dd4ec6

Please sign in to comment.
Something went wrong with that request. Please try again.