Hi all,
I use in my web application a recursive model like that :
########################################
class Category(models.Model):
title = models.CharField(verbose_name=_("title"), unique=True,
db_index=True, max_length=255)
slug = models.SlugField(verbose_name=_("slug"), db_index=True,
unique=True)
description = models.TextField(verbose_name=_("description"),
blank=True)
parent = models.ForeignKey("self", verbose_name=_("parent"),
null=True, blank=True)
def __unicode__(self):
if self.parent:
return "%s -> %s" % (self.parent, self.title)
else:
return self.title
def html(self):
if self.parent:
return """%s <img src="/media/img/public/arrow_right.png"
alt="->"/> %s""" % (self.parent.html(), self.title)
else:
return self.title
class Meta:
verbose_name_plural = _("Categories")
########################################
The problem is when i have a category with a lot of parents
categories, like that :
Computing -> Programming -> Python -> Django
Django does 4 SQL Queries to get that, one per parent category, the
queries are like :
########################################
SELECT "blog_category"."id", "blog_category"."title",
"blog_category"."slug", "blog_category"."description",
"blog_category"."parent_id"
FROM "blog_category"
WHERE "blog_category"."id" = 9
########################################
How can i optimize that do to that with a kind of select_related ? can
i use it in that situation ? or something else ?
Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---