This model allows for site-map style menu hierarchies in the admin
interface:

top level
-subpage
--another subpage
-subpage2


It feels snappy, but I bet it's inefficient due to the fact that it is
recursively calling self.parent. I imagine that there's a way to
optimize it.

---

class MenuManager(models.Manager):
  def get_query_set(self):
      return super(MenuManager,
self).get_query_set().order_by('parent', 'name')

class Menu(models.Model):
  name = models.CharField(maxlength = 100)
  parent = models.ForeignKey('self', null=True, blank=True)
  objects = MenuManager()

  def __str__(self):
    return ('-' * self.depth()) + self.name

  def depth(self):
    if not self.parent:
      return 0
    else:
      return 1 + self.parent.depth()
  
  class Admin:
    pass


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to