views:

160

answers:

1

I'm getting a non-descriptive (or at least I don't know how to interpret in this context) error message when sub-lassing from a Django-Treebeard node and am not sure how to debug. I'm using the installation instructions at: http://code.tabo.pe/django-treebeard/src/tip/tbexample/ (see at end of posting).

I create a subclass of MP_Node and the syncdb works. However, loading the models.py code into a shell produces a "list index out of range" error - see code and trace below.

Thanks for your help.

Python 2.6.4, Django 1.1, Treebeard 1.1:

try:
    from django.db import models, transaction
    from django.db.models import AutoField
    import django.dispatch
    from django.contrib.treebeard.mp_tree import MP_Node
except ImportError, exc:
    print "django error in %s: %s" % (__file__, exc)

class DelibNode(MP_Node): pass

Traceback (most recent call last):
     File "<console>", line 1, in <module>
     File "C:\Program Files\Python26\lib\site-packages\django\db\models\base.py", line 52, in __new__
     kwargs = {"app_label": model_module.__name__.split('.')[-2]}
     IndexError: list index out of range

Installed Apps in Settings.py:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.s  ites',
'django.contrib.admin',
'django.contrib.treebeard',
'medCE.delib'
)

Instructions:
1. Run easy_install django-treebeard to install the
latest treebeard version from PyPi
1.1. If you don't like easy_install, download a release from the
treebeard download page or get a development version
from the treebeard mercurial repository and run
python setup.py install
2. Add 'treebeard' to the INSTALLED_APPS section in your
django settings file.
3. Create a new model that inherits from one of django-treebeard's
abstract tree models: mp_tree.MP_Node (materialized path),
ns_tree.NS_Node (nested sets) or al_tree.AL_Node
(adjacency list).
4. Run python manage.py syncdb

A: 

You can browse the Django source-code online:

http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py

The relevant code that throws the exception has this comment:

# Figure out the app_label by looking one level up.
# For 'django.contrib.sites.models', this would be 'sites'.

So it seems that the code is trying to determine the app that a model belongs to.

To debug this you could simply modify the base.py to catch the IndexError and raise the model_module._name_.

prometheus
Thanks for your response.I have seen the app_label comment is some blog. That's why I moved the treebeard code to be under the django/contrib directory from initially being simply under lib/site-packages. I'm still getting the same error. I don't understand what app_label its looking for and why it wouldn't be able to find it - I'm trying to follow the most basic installation instructions and most vanilla sub-classing of an MP_Node and yet nobody else seems to be reporting this problem.
I shouldn't need to modify code in any way but just in case I don't get another answer can you elaborate on your suggestion:To debug this you could simply modify the base.py to catch the IndexError and raise the model_module._name_.I'm new to both Python and Django. Thanks!
The class it was crashing on was '__builtin__'. I changed the code as per your suggestion: print "base.ModelBase> model_module", model_module # debug try: one_up = model_module.__name__.split('.')[-2] except IndexError: one_up = "treebeard" kwargs = {"app_label": one_up}Now it works but I still have no idea why a vanilla installation based on instructions wouldn't work - I'd think I'd find some mention of it somewhere.