Django: Get all model instances of top level category
Question: I've got a heirarchy like so:

    
    
    Parent Cat
         Sub Cat
             Item
         Sub Cat
             Item
             Item
    Parent Cat
    ...
    

Having the name/instance of a parent category I'd like to get all items which
are related to the parent cat. I'm currently using Django-Categories which
makes use of Django-MTPP but I can't figure out how to do this without
building the for loops myself. Thought the whole point of these packages were
so I wouldn't have to do that.

My models look like:

    
    
    class Item(models.Model):
        title = models.TextField() # `null` and `blank` are false by default
        category = models.ForeignKey('ProductCategory')
        price = ....
    
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.title
    
    from categories.models import CategoryBase
    
    class ProductCategory(CategoryBase):
    
        class Meta:
            verbose_name_plural = 'simple categories'
    

I've tried doing:

    
    
    parent_category = ProductCategory.objects.get(slug=parent_cat, parent=None)
    items = parent_category.item_set.all()
    

But that doesn't produce any results.


Answer: You should filter your items:

    
    
    items = Item.objects.filter(category__parent=parent_category)
    

The double score is used to follow model relationships or to use more complex
lookups than a simple equal. Documentation
[here](https://docs.djangoproject.com/en/1.5/topics/db/queries/#lookups-that-
span-relationships)

