Getting descendants of a model instance in Django by mraziz

For this website, I have a model called Category defined as follows:

class Category(models.Model):
    name = models.CharField(max_length=64)
    parent = models.ForeignKey('self', null=True, blank=True)

I wanted at some point to retrieve all the descendats of a certain category, unfortunately Django ORM doesn't have this feature out of the box (yet), so I had to create my own. The idea is create a recursive function that will collect a list of all the siblings at a certain depth and then move on to depth+1 until we get to the leaves.

def get_children(category):
    nodes = Category.objects.filter(parent__in=category)
    if nodes:
        yield nodes

categories = [category,] + [node for x in get_children([category,]) for node in x]