尝试根据说明安装 Django-Treebeard 时出现异常

发布于 2024-08-20 16:40:28 字数 1822 浏览 5 评论 0原文

当从 Django-Treebeard 节点进行子集时,我收到一条非描述性的(或者至少我不知道如何在这种情况下解释)错误消息,并且不确定如何调试。我正在使用安装说明:http://code.tabo。 pe/django-treebeard/src/tip/tbexample/ (参见发布末尾)。

我创建了 MP_Node 的子类,并且syncdb 可以工作。但是,将 models.py 代码加载到 shell 中会产生“列表索引超出范围”错误 - 请参阅下面的代码和跟踪。

感谢您的帮助。

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

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'
)

说明:
1. 运行 easy_install django-treebeard 安装
PyPi 的最新 Treebeard 版本
1.1.如果您不喜欢 easy_install,请从
下载版本 Treebeard 下载页面或获取开发版本
从 Treebeard Mercurial 存储库并运行
python setup.py install
2. 将“treebeard”添加到
INSTALLED_APPS 部分 Django 设置文件。
3. 创建一个继承自 django-treebeard 的新模型
抽象树模型:mp_tree.MP_Node(物化路径),
ns_tree.NS_Node(嵌套集)或 al_tree.AL_Node
(邻接表)。
4.运行python manage.pysyncdb

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-08-27 16:40:28

我今天也犯了同样的错误。要修复您首先转到 models.py 文件,并在每个类中您必须添加另一个类:

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

我认为您收到此错误是因为应用程序名称中的句点。如果 Meta 类中没有提供应用程序名称,Django 将尝试通过分解文件夹结构自行找出它。当发生这种情况时,它会在“句点位置”分解,并在您的情况下计算出应用程序名称为 medCE 或 delib,这显然不是您的应用程序名称。

我知道这个问题已经很老了,但希望它能帮助未来的观众

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

拥抱影子 2024-08-27 16:40:28

您可以在线浏览 Django 源代码:

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

引发异常的相关代码有这样的注释:

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

所以看来代码正在尝试确定模型所属的应用程序。

要调试此问题,您可以简单地修改 base.py 以捕获 IndexError 并引发 model_module.__name__ 。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文