Michael Ellis wrote: > Hello all, > > I have the following models: > > class Category(models.Model): > name = models.CharField(core=True, max_length=100) > parent = models.ForeignKey('self', blank=True, null=True, > related_name='child') > > class Product(models.Model): > name = models.CharField('name', max_length=200) > category = models.ForeignKey(Category, blank=False, null=False) > > Some products belong to top-level categories and some belong to sub- > categories. Some categories have no sub-categories. > > I want to create a list of all categories, sub-categories, and their > related products ONLY IF a product exists in that category. If a > category exists but there are no products in it, I don't want to list > it. Something like this: > > Cat 1 > Subcat 1 > prod 1 > prod 2 > Subcat 2 > prod 3 > Cat 2 > prod 4 > prod 5 > > Any suggestions would be greatly appreciated. > > I've been thinking a bit about this. I thought I would leave it for it a bit to see if anyone else replied, as I'm no expert and pretty new to all this, but the way I was thinking about it is:
* one question is whether to try to do it all in a single database query, or to construct the table in memory using a series of queries, and create some kind of tree like data structure to represent the results, then pass this to the template. I think the first would be quite difficult, though might somehow be possible in raw SQL, so you'll probably have to do the second. * then the question is whether to start with the products and work 'up' to the top level category, or start with the top level category and work 'down' to the products. The first way works best with standard ways of doing recursion, but would mean writing code to eliminate empty categories, whereas if you started with the products, you would automatically return only the results you want. Having decided how to do this, you've then got the problem of: * how to write a template that can render a tree data structure. I'm not sure that this is possible without writing a custom tag. One way of doing it would be to rewrite or subclass the 'include' tag to allow extra parameters to be passed into the include which would only be valid inside that specific include. I.e. if you had a data structure which used 'Cat' classes like this: class Cat: name="category name" sub_cats=[...list of sub categories...] products=[...list of products...] you want a tag that would let you write an include file like: {% for category in categorybranch %} (print category name here) {% include "self.html" with categorybranch=category %} {% for product in categorybranch.products %} (print product name here) {% endfor %} {% endfor %} Then you could have a template which recursively included itself, and passed a parameter to say which branch of the tree to render next. But I'm not sure how this would work with django's way of rendering templates. Just my thoughts, andy. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---