Exception when trying to install Django-Treebeard based on instructions

喜欢而已 提交于 2019-12-08 03:15: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 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


回答1:


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




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



来源:https://stackoverflow.com/questions/2215403/exception-when-trying-to-install-django-treebeard-based-on-instructions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!