django-models - Django 模型 "IndexError: list index out of range"Pydev

标签 django-models

我在 Eclipse PyDev 中有一个 Django 项目。

我有一个文件 views.py 有一行:

from models import ingredient2

在models.py中我有:
from django.db import models
class ingredient2(models.Model):
     ingredient     = models.CharField(max_length=200)

当我尝试运行该应用程序时,出现以下错误:
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 54, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

我确实同步了数据库并启动了服务器运行。

我进入 base.py 并添加了 2 个打印语句(是的,我可能不应该编辑 Django 的文件):
if getattr(meta, 'app_label', None) is None:
            # Figure out the app_label by looking one level up.
            # For 'django.contrib.sites.models', this would be 'sites'.
            model_module = sys.modules[new_class.__module__]
            print model_module #ADDED
            print model_module.__name__ #ADDED
            kwargs = {"app_label": model_module.__name__.split('.')[-2]}

他们打印出来:
<module 'models' from 'C:\Users\Tine\workspace\slangen\slangen2\bolig\models.pyc'>

models

manage.py 包含在 bolig 文件夹中。我认为正确的应用标签应该是“bolig”。该应用程序在几个月前工作,现在,当我回到它时,有些不对劲。我一直在 PyDev 中创建其他项目。

最佳答案

在模型类定义中添加一个带有 app_label 的元类:

class Foo:
    id = models.BigIntegerField(primary_key=True)
    class Meta:
        app_label = 'foo'

关于django-models - Django 模型 "IndexError: list index out of range"Pydev,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8754561/

相关文章:

python - 如何在Django中手动定义遗留数据库的一对一键

django - React JSX 和 Django 反向 URL

python - Django 中的抽象基类模型与代理模型

python - Django Rest Framework - CreateAPIView ForeignKey 查找字段

python - 使用最大值过滤数据

django - 如何在 Django 管理区显示多对多关系字段(如果多对多关系使用 'through' )?

python - 你如何在 Django 聚合中动态分配别名?

mysql - 当行不是通过 ORM 创建时,Django 根据外键删除行的速度很慢

django - 为什么 Django 中没有 "ListField"?

python - Django 在更新时触发 post_save()