def get_parent(self): if self.parent: return [self.parent]
return [] def get_parent_tree(self): asc = self.get_parent() for p in asc: asc.extend(p.get_parent()) return asc This is what I use for generating a category crumb list Dj Gilcrease OpenRPG Developer ~~http://www.openrpg.com On Wed, Oct 29, 2008 at 4:28 AM, coderb <[EMAIL PROTECTED]> wrote: > > hi Thomas, > > thanks for all the details, actually my post was not really clear on > what I have. > the category model looks like this: > > class Category(models.Model): > name = models.CharField(max_length=100, unique=True) > slug = models.SlugField(max_length=100, unique=True) > parent = models.ForeignKey('self',blank=True,null=True) > seq = models.IntegerField(default=10) > active = models.BooleanField(default=1) > > def __unicode__(self): > return self.name > > @models.permalink > def permalink(self): > return "barter.listings.views.category", [self.slug] > > --------------- > > my view looks like this: > > def category(request, myslug): > category_list = Category.objects.filter(parent__slug=myslug) > catcrumb_list = Category.objects.filter(slug=myslug) > return render_to_response('listings/browse.html', { > 'category_list': category_list, > 'catcrumb_list': catcrumb_list, > } > ) > > > and my template looks like this: > > {% extends "base.html" %} > > {% block content %} > <h2>Browse Categories</h2> > <div class="crumbs"> > {% if catcrumb_list %} > {% for crumb in catcrumb_list %} > <a href="{{ category.permalink }}">{{crumb.name }}</a> > {% endfor %} > {% endif %} > </div> > {% if category_list %} > <ul> > {% for category in category_list %} > <li><a href="{{ category.permalink }}">{{category.name }}</ > a></li> > {% endfor %} > </ul> > {% else %} > <p>No Categories are available.</p> > {% endif %} > {% endblock %} > > > currently in this code, catcrumb_list is only populated with the last > parent category, so the nest-parents is what I need. > your example above looks good, I'll give it a try and let you know, > still need to go through your attached trees.py. > > One question though, since the array is being built in reverse order > (child to parent), when it comes to looping thru it in the template, > it will be printed in the wrong order, so I guess I need to reverse it > before hand or instead of using append > could I use something like > > parents.insert(1, cat_crumb) > > thanks again for your help > > > > On Oct 29, 7:25 am, Thomas Guettler <[EMAIL PROTECTED]> wrote: >> 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 >> >> tree.py >> 2KViewDownload > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---