我在运行自定义命令时遇到问题,因为它会抛出一个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)
我将应用名称添加到了安装的应用程序中。它在我在自定义命令中运行打印输出时有效。但是一旦我为我的模型添加了import语句,它就会抛出NameError。我可以知道如何解决这个问题吗?
答案 0 :(得分:2)
您似乎忘记了使用文件名字符串的引号。尝试:
self.loadTagTrend('graphofknowledge/tagTrend_refine.txt')