def get_descendant_ids(root_id):
 children = Page.objects.filter(parent=root_id).values_list(u'pk', flat=True)
 for child_id in children.iterator():
  (yield child_id)
  for descendant_id in get_descendant_ids(child_id):
   (yield descendant_id)

def get_descendant_ids ( root_id ) : children = Page . objects . filter ( parent = root_id ) . values_list ( u'pk' , flat = True ) for child_id in children . iterator ( ) : ( yield child_id ) for descendant_id in get_descendant_ids ( child_id ) : ( yield descendant_id )
returns the a generator of primary keys which represent descendants of the given page id .