Exception when trying to install Django-Treebeard based on instructions
Asked Answered
C

2

2

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

Chader answered 7/2, 2010 at 0:45 Comment(1)
I also get the same error when the treebeard directory is not installed under django/contrib but under site-packages directly.Chader
C
3

I had the same error today. To fix you first go to your models.py file and in each of the classes you have to add another class:

class Meta:
    app_label = 'app_name' # medCE.delib in your case

I think that you are getting this error because of the period in your app name. If an app name is not provided with the Meta class, Django will try to figure it out by itself by decomposing the folder structure. When that happens, it decomposes at the 'period location' and figures out the app name to be medCE or delib in your case, which is obviously not your app name.

I know the question is old, but hopefully it'll help future viewers

Colony answered 11/7, 2012 at 17:48 Comment(0)
T
2

You can browse the Django source-code online:

https://github.com/django/django/blob/master/django/db/models/base.py#L90

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__.

Trevar answered 7/2, 2010 at 10:28 Comment(3)
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.Chader
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!Chader
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.Chader

© 2022 - 2024 — McMap. All rights reserved.