All children of each ancestor without looping?

65 views
Skip to first unread message

Daniele Procida

unread,
Nov 24, 2011, 8:09:09 AM11/24/11
to django-...@googlegroups.com
I am sure there must be a way to reduce my looping over a list to a much more efficient query, but I can't work it out.

Here's my current code:

# get pages
pages = Pages.filter(some filters)

# get a queryset of ancestors
ancestry = current_page.get_ancestors(ascending = True)

# create a queryset of relevant pages
relevant_pages = pages & ancestry

# create a list from this set
page_list = list(relevant_pages)

# extend the list with the children of each
for page in relevant_pages:
page_list.extend(page.get_children() & pages)

pages = page_list


In other words, give a queryset of objects called "pages", and a page called "page", I want to get all the children of each ancestor of "page" that exists in pages.

Is this possible?

Thanks!

Daniele

Reply all
Reply to author
Forward
0 new messages