Hi, Does this help you?
myslug=... parents=[] while myslug: cat_crumb=Category.objects.get(slug=myslug) # I guess slug is unique parents.append(cat_crumb) myslug=cat_crumb.parent But be careful, you might get infinite loops if the parent links to a child. To avoid this you could write an "assert len(parents)<100" into the loop. Except you have a really deeply nested categories. BTW, Why is "parent" a slug field and not a foreign key? I attached my tree mix in. coderb schrieb: > sorry, had not finished this post before accidently submitting it... > > for myslug not null > catcrumb_list = Category.objects.filter(slug=myslug) > myslug = catcrumb_list.parent > > basically, I want to loop through the category model a parent is not > found, and each pass, store the model tupel in catcrumblist. > > I tried catcrumb_list.append, but returns read-only error on 'list' > > any help would be apprec-ated. > > thank you > -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
# -*- coding: iso-8859-1 -*- # $Id: tree.py 148 2008-10-15 08:19:10Z tguettler $ # $HeadURL: svn+ssh://svnserver/svn/djangotools/trunk/utils/tree.py $ empty_magic=object() class Tree(object): u''' Model Mix-In Usage: class TreeTag(models.Model, tree.Tree): parent=models.ForeignKey('self', related_name='children', null=True, blank=True) name=models.CharField(max_length=64) ... ''' MAX_REC_DEPTH=1000 def __unicode__(self, rec_depth=0): if not self.parent: return self.name if rec_depth>self.MAX_REC_DEPTH: raise AssertionError('Tree is a loop') return '%s / %s' % (self.parent.__unicode__(rec_depth+1), self.name) def parents(self, me_too=False): iloop=0 parent=self.parent parents=[] if me_too: parents.append(self) while parent: iloop+=1 assert iloop<self.MAX_REC_DEPTH, 'Tree is a loop' parents.append(parent) parent=parent.parent return parents def get_children(self, children=None, me_too=False, rec_depth=0, ids=False): assert rec_depth<self.MAX_REC_DEPTH if children==None: assert rec_depth==0 children=set() else: assert rec_depth>0 for child in self.children.all(): if ids: children.add(child.id) else: children.add(child) child.get_children(children, rec_depth=rec_depth+1) if me_too: if ids: children.add(self.id) else: children.add(self) return children def test(self): self.parents() # raises Exception if infinit loop @classmethod def str_to_object(cls, object_str, default=empty_magic): ''' Gibt zu einer Zeichenkette 'Foo / ...' die entsprechend Objekt (Belegart/Tag) zurück. Falls die Objekt unbekannt ist, wird None, zurückgegeben. ''' mylist=object_str.split('/') mylist.reverse() kwargs={} level=0 for name in mylist: name=name.strip() assert name kwargs['%sname' % (level*'parent__' )]=name level+=1 assert level kwargs['%sisnull' % (level*'parent__')]=True try: obj=cls.objects.get(**kwargs) except cls.DoesNotExist: if default==empty_magic: raise cls.DoesNotExist(unicode(kwargs)) return default return obj