2016-02-29 88 views
0

我在运行自定义命令时遇到问题,因为它会抛出一个NameError:全局名称“graphofknowledge”未定义。我的文件结构是Django管理命令无法找到我的应用程序

 
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) 

我增加了应用程序的名称到安装的应用程序的代码。当我在自定义命令中运行打印输出时,它工作正常。但是,一旦我为我的模型添加import语句,它将引发NameError。我可以知道我该如何解决这个问题?

+0

嗨,你可以请粘贴异常堆栈跟踪?哪个Django和python版本? –

+0

@Mauro Rocco python 2.7和django 1.5.6 – LeonBrain

+0

请将您的堆栈跟踪添加到问题中,否则没有人能够在这里提供帮助。 –

回答

2

它看起来像你忘记了你的文件名字符串使用引号。尝试:

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

谢谢,我没有发现我愚蠢的错误! – LeonBrain