Django 管理命令无法找到我的应用程序

Django management commands is not able to find my application

我的自定义命令 运行 有问题,因为它会抛出 NameError:全局名称 "graphofknowledge" 未定义。我的文件结构是

projectFile
|-manage.py
|-..
|-graphofknowledge (app)
  |-models.py
  |-views.py
  |-management
    |-__init__.py
    |-commands
      |-__init__.py
      |-customCommand.py

这是我的自定义命令的代码

from django.core.management.base import BaseCommand
from graphofknowledge.models import TagTrend_refine
class Command(BaseCommand):
    args = '<foo bar ...>'
    help = 'our help string comes here'
    def loadTagTrend(self, fileName):
        listOfData = []
        f = open(fileName)
        lines = f.readlines()
        f.close()
        for line in lines:
            temp = line.strip().split("\t")
            data = TagTrend_refine(
            tag = temp[0],
            trendData = temp[1]
            )
            listOfData.append(data)
        TagTrend_refine.objects.bulk_create(listOfEntities)

    def handle(self, *args, **options):
        self.loadTagTrend(graphofknowledge/tagTrend_refine.txt)

我在已安装的应用程序中添加了应用程序名称。当我 运行 在自定义命令中打印出来时它起作用了。但是一旦我为我的模型添加了导入语句,它就会抛出 NameError。我可以知道如何解决这个问题吗?

您似乎忘记了为文件名字符串使用引号。尝试:

    self.loadTagTrend('graphofknowledge/tagTrend_refine.txt')