开发者

Exception when trying to install Django-Treebeard based on instructions

开发者 https://www.devze.com 2022-12-19 22:17 出处:网络
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 ins

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 dow开发者_如何学编程nload 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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消