I have a model for tree like data (id - parent). And I have recursion method, to get all children for current page.
children = []
class Page(models.Model):
...
def get_children(self, include_self=True):
global children
if include_self:
children.append(self.id)
for c in Page.objects.filter(parent=self.id):
c.get_children(True)
return children
I use console for a test:
>>> from pages.models import Page
>>> q = Page.objects.get(id=10) #I get a page with children.
>>> q.get_children(False)
It works fine:
[12L, 11L]
But if I use this method next time,
q.get_children(False)
result will be:
[12L, 11L, 12L, 11L]
I want always have:
[12L, 11L]
What is do wrong? How I should reset the variable but safe scope of recursion?
EDIT:
Correct recursion function is:
def get_children(self, include_self=True, children = None):
if children is None:
children = []
if include_self:
children.append(self.id)
for c in Page.objects.filter(parent=self.id):
c.get_children(True)
return children