I am trying to write a simple algorithm to get the 'rank' of a django mptt model. By 'rank' I mean the number of levels of descendents... similar to MPTTModel.get_level() but counting from leaf to root.
My approach is the following:
views.py
def index(request):
nodes = Node.objects.all()
nodesets = []
for i in nodes:
figure_rank(i, i)
if i.count >= 3:
nodeset = Node_set(node=i, name=i.name)
nodeset.save() # This is where the error is caught
nodesets.append(dataset)
return render(request, 'associate/index.html', {'nodesets':nodesets})
def figure_rank(over, under):
if under.get_children()[0]:
r = under.get_children()[0]
over.count += 1
figure_rank(over, r)
else:
return over.count
For nodes with rank >=3 I want to create a Node_set and return a set of nodes once they are all given ranks.
I get the following error:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/associate/
Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'associate',
'images_app',
'mptt')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\djangoprojects\images\associate\views.py" in index
9. figure_rank(i, i)
File "C:\djangoprojects\images\associate\views.py" in figure_rank
23. figure_rank(over, r)
File "C:\djangoprojects\images\associate\views.py" in figure_rank
23. figure_rank(over, r)
File "C:\djangoprojects\images\associate\views.py" in figure_rank
20. if under.get_children()[0]:
File "C:\Python27\lib\site-packages\django\db\models\query.py" in __getitem__
132. return list(qs)[0]
Exception Type: IndexError at /associate/
Exception Value: list index out of range
What am I doing wrong here?
You need to properly check if there are no children. In figure_rank
, under.get_children()
is returning an empty list, and then you try to access the first element. Check if the list is empty first.
def figure_rank(over, under):
children = under.get_children()
if chilren: # check if children is nonempty before accessing the first element
r = children[0]
over.count += 1
figure_rank(over, r)
else:
return over.count