Commit cdb2c3a8 by Bridger Maxwell

Added a bit of caching to retrieving path, so it doesn't make so many sql queries.

parent 6c9e1a03
......@@ -32,13 +32,17 @@ class URLPath(MPTTModel):
@property
def path(self):
if not self.parent: return ""
return "/".join([obj.slug if obj.slug else "" for obj in self.get_ancestors(include_self=True).exclude(parent=None)]) + "/"
if not hasattr(self, '_cachedpath'):
self._cachedpath = "/".join([obj.slug if obj.slug else "" for obj in self.get_ancestors(include_self=True).exclude(parent=None)]) + "/"
return self._cachedpath
@classmethod
def root(cls):
site = Site.objects.get_current()
root_nodes = cls.objects.root_nodes().filter(site=site)
no_paths = root_nodes.count()
root_nodes = list(cls.objects.root_nodes().filter(site=site))
# We fetch the nodes as a list and use len(), not count() because we need
# to get the result out anyway. This only takes one sql query
no_paths = len(root_nodes)
if no_paths == 0:
raise NoRootURL("You need to create a root article on site '%s'" % site)
if no_paths > 1:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment